Firebase Cloud Messaging with $cordovaPush, how to work also for iPhone?

Asked

Viewed 53 times

0

I am emulating my application by Intel XDK for IOS. I use it by working with Windows and it emulates and gives the error messages very well.

The first mistake, is that when the user logs in, he takes the Cloud Messaging senderID and logs the GCM in my database to send the notifications according to the rules of my app.

However, on line 29, which is:

$cordovaPush.register(androidConfig).then(function (result) {

Obviously gives an error, because I’m not using Android, I want to use iOS too. How can I adapt my code to use with iOS too?

Follow the full code:

$scope.register_gcm = function () {

            $ionicLoading.show();
            var androidConfig = {
                "senderID": "MEU_ID_DO_GOOGLE_MESSAGE",

            };


            document.addEventListener("deviceready", function () {
                //            alert("device ready");
                $cordovaPush.register(androidConfig).then(function (result) {
                    // Success
                }, function (err) {
                    $ionicLoading.hide();
                })

                $rootScope.$on('$cordovaPush:notificationReceived', function (event, notification) {
                    //                 alert("Passa do rootScope.on");
                    $ionicLoading.hide();
                    switch (notification.event) {
                        case 'registered':
                            if (notification.regid.length > 0) {
                                console.log(notification.regid);

                          //      alert(notification.regid);

                                $http.get("http://MEUSITE_ONDE_FACO_UPDATE_NA_TABELA_DE_USUARIO.com.br/admin/apis/push_config_vovo/set_gcmkey.php?user_id=" + window.localStorage.getItem("user_id") + "&gcm_key=" + notification.regid)
                                    .then(function (result) {
                                        console.log("printo the result.data: " + result.data);
                                //        alert(result.data);
                                        window.localStorage.setItem("gcm", notification.regid);
                                //        alert("GCM REGISTERED!!");
                                        $ionicLoading.hide();
                                    }, function (result) {
                                        console.log(result);
                                        $ionicLoading.hide();
                                    })



                            }
                            break;

                        case 'message':
                            // this is the actual push notification. its format depends on the data model from the push server
                            //alert('message = ' + notification.message + ' msgCount = ' + notification.msgcnt);
                            break;

                        case 'Erro':
                            alert('GCM error = ' + notification.msg);
                            break;

                        default:
                            alert('Nenhum evento GCM foi encontrado');
                            break;
                    }
                });

            }, false);


        }

Works great for Android, but how to make it work also on IOS?

1 answer

2


From what I understand you need to pass the settings according to the platform. So you need to check if it is IOS/Android and then pass the parameters to the $cordovaPush.register. For this you can use the https://cordova.apache.org/docs/en/latest/reference/cordova-plugin-device/index.html.

  // CONFIGS
  var pushConfig = {
    android: {
      "senderID": "ID_GOOGLE",
    }, 
    ios: {
      "senderID": "ID_IOS",
    }
  };

  // SELECIONAR CONFIG DE ACORDO COM PLATFORM
  function getPushConfig() {
    return pushConfig[device.platform.toLowerCase()];
  }

  document.addEventListener("deviceready", function () {
      $cordovaPush.register(getPushConfig()).then(function (result) {

Browser other questions tagged

You are not signed in. Login or sign up in order to post.