Push notifications problem as of february 2016 new problem because of updating

Okay people. This entire process changed and now suddenly errors.

First of all this page now says to install the phonegappush plugin. Before it said to create a new project with the push template. (i.e. ionic start project_name push). Have a look at it now. It is different. You know when you release a new version of ionic one thing you should be doing is to test push notifications since many thousands of apps will no longer work anymore and developers will be very angry with you for not thinking it through. Okay perhaps you did test it. Fine. Make sure you update the intructions for how to register for push when the app is loaded or when the user logs in or whenever you want us to be doing this now. See the above link step #5. WTF is that? It does nothing when the app is loaded. I get nothing in the console log but the annoying

"TypeError: Cannot read property ‘pushNotification’ of undefined

This page indicates that the push plugin you were using is now deprecated and the instructions above say you are moving forward with the phonegappush plugin instead. Fine but the instructions on this page are now not complete. You have the setup of the keys on the google developers console and the apple keys of which I do not need any help and I am very well aware of how this works. It was working before you decided to change things. Now that it doesn’t work anymore (I stupidly decided to let MacOSX update Xcode to 7.2 and now I am screwed). I guess I have to watit around for a fix at ionic or the instructions page to be updated. Of course you will not do this because you will disagree with me even though I am right. I am very sorry to speak like this but it is very sloppy work on your part to do stuff like this. It has been in GA since early last year. And you change push notifications frequently. They don’t need to be changed, but you change them. All we want is to just see messages pop up on both iOS and Android and done. There is no need to get fancy with this. Why do you think it needs to change this often.

Thank you ionic for wasting my time.

Fine whatever. Typically what I do when I ahve problems is to recreate a new project from scratch from the latest instructions.

1 Like

I get that you’re frustrated but this is a FREE framework that a ton of people have put a lot of time into. I’m struggling with a similar issue, but while also frustrated I try to keep it civil and let my angst out elsewhere, please for the respect of others try and do the same.

If you do not have anything constructive to say then please just shut up.

What I said was constructive, in that it attempts to help keep the experience useful and enjoyable for all involved. You think you’re the only one who gets frustrated? This isn’t the place for your vitriol.

By constructive I meant something that solves the problem. The purpose of this forum. Duh.

Other people’s enjoyment and happiness is completely irrelevant. Hell even my own happiness is irrelevant. The only thing that matters is my work and the advancement of my own mind.

As for me thinking I am the only developer that is frustrated… Being one of the best that is out there I imagine most developers would have more problems and more frustrated than me. So you made an incorrect statement. Anyway whatever. I am done with this conversation don’t bother responding.

EDIT: This method intended to be used with your own server, not with Ionic.io services. I use my own servers and connect them directly to Google Cloud Messaging (free service) that pushes my notifications to both my Android and iOS users. This allows me to have full control over all the notifications that are being sent.

Hi there,

I hope this might help you somehow.

  1. Install the following plugin: https://github.com/phonegap/phonegap-plugin-push

  2. Create a service that registers and unregisters the device with Google Cloud Messaging (GCM)

     .service('PUSH_NOTIFICATIONS', function($http, $ionicPlatform, $cordovaDevice, $q){
    
     var _registered = false, _inprogress = false, _push, _gcm_id, _device;
    
     this.init = function(){
         _inprogress = true;
         $ionicPlatform.ready(function(){
    
             // If already registered or not signed in
    
             if(_registered || ['Android','iOS'].indexOf($cordovaDevice.getPlatform()) == -1) return; // Ensure that we're running on Android or iOS
    
             // Push Notifications Init
    
             var push = PushNotification.init({
                 android: {
                     senderID: "1234567890" // GCM Sender ID (project ID)
                 },
                 ios: {
                     alert: "true",
                     badge: "true",
                     sound: "false",
                     senderID: "1234567890" // GCM Sender ID (project ID)
                 }
             });
    
             _push = push;
    
             push.on('registration', function(data){
                 _gcm_id = data.registrationId;
    
                 // Post our GCM and device information to our API
                 $http.post('http://MYAPIURL.com/registerpn', {
                     gcm_id: data.registrationId,
                     device: $cordovaDevice.getDevice()
                 }).then(
                     function(res){
                         console.log("Registered on API...");
                         _registered = true;
                         _inprogress = false;
                     },
                     function(err){
                         console.log("error registering for PN", err);
                         _inprogress = false;
                     }
                 );
             });
    
             push.on('notification', function(data){
                 // Here is what we will do if we get a push notification while using the app
                 console.log("GOT PN", data);
             });
    
             push.on('error', function(err){
                 console.log("PNR Error", err);
             });
         });
    
    
    
     };
     this.unregister = function(){
         return $q(function(resolve, reject){
             _push.unregister();
             return $http.post('http://MYAPIURL.COM/unregisterpn', {device: _device, gcm_id: _gcm_id})
                 .then(
                     function(success){
                         _registered = false;
                         resolve();
                     },
                     function(err){
                         // We didn't manage to inform the server that we unregistered
                         console.log("Error unregistering");
                         resolve();
                     }
                 );
         });
     };
     })
    
  3. In my case, my application has user authentication. So whenever the user logs in I call the function PUSH_NOTIFICATIONS.init(). So if you don’t have user authentication and you would like to register every user then just call this function inside your .run()

3 Likes

This is great thanks. Actually I am doing user authentication. So yes it has to be done after the user logs in. Or clicks the activate user link from their email inbox linking into my application.

I will of course do something very similar to this. I just didn’t know what the javascript should look like to interact with the new version of the push plugin. Basically I look at my own code okay and people on my team (when I am on a team) but I quickly get confused having to look at code in the framework and knowing precisely what will be called and in which file.

How did you figure this out? Which file in the ionic framework is this defined in. I searched for it and found thousands of references and I don’t have time to look through it all. I am assuming you figured it out by looking at the framework code. That is since this is not documented anywhere.

I didn’t look at Ionic Framework code to figure this out since it is not really related to Ionic framework.

When I was trying to add push notifications I stumbled upon the link you shared in your original post along with a few other outdated pages such as ngCordova’s site. And I was as frustrated as you are with the outdated information… so I decided to dig through the original plugin’s documentation which explains how to implement the functions on any Cordova based application.

The documentation can be found here: https://github.com/phonegap/phonegap-plugin-push/tree/master/docs

After testing out many things with the documentation and setting up my GCM services correctly, I used $cordovaDevice plugin to get information about the current device … such as serial number and platform … etc.

And most importantly you need to wrap the whole functionality with the device.ready listener that ensures the Cordova library and all plugins are successfully loaded. Hence why I used $ionicPlatform.ready().

1 Like

What are you sending to GCM from your server, I can’t seem to get that part of it to work for ios/android?

I’m also experiencing this issue, after adding the ionic-web-platform plugin (as part of the tutorial) my app would no longer load in emualtors and on devices, I’ve verified this by copying over my code to a new project which works completely perfectly until I run ‘ionic add ionic-web-platform’ etc etc, and to make matters worse, the plugin isn’t properly removed from ionic.bundle.min.js after running ‘ionic remove ionic-web-platform’ command.

Would really like an updated tutorial/fix for this so I can use push notifications in my project!

Have you solved your issue? I’m stucked! =(

Hi,

I tried this code, it is working fine and received notification in Android and iOS. But when I click push notification its redirect to index page. I need to redirect to specific page in my app.
I used $state.go(‘aaaa’) inside push.on(‘notification’, function(data){) method, it doesn’t work. please let me know which method will trigger while click the push notification?

Here is my code.

push.on(‘notification’, function(data){
// Here is what we will do if we get a push notification while using the app

			console.log("GOT PN", data);

});

Only little corection. Sender ID is not Project ID. Sender ID is project number.
Project number is available at https://console.developers.google.com/iam-admin/settings/