activitree / meteor-push

Meteor Push Notifications for Cordova and Web/PWA with Firebase (FCM).
MIT License
27 stars 19 forks source link

How to route to a specific place on click of push notification? #15

Closed jamauro closed 4 years ago

jamauro commented 4 years ago

Thanks for the v2 update! I was able to put a urlPath that I want to route to when the user clicks on the push notification in data but I'm not sure how to read that urlPath and let my router handle it.

Can you point me in the right direction?

Here's what I have in client/startup.js

// Configure CordovaPush inside Meteor.startup
CordovaPush.on('startup', notification => {
      console.log("notification: ", notification);
      // if there's a urlPath route to it
      if (notification.data && notification.data.urlPath) {
         Router.go(notification.data.urlPath);
     }
});
paulincai commented 4 years ago

Hi,

please check this: https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/API.md#pushonnotification-callback

With activitree:push your handler looks like the following, and you were right to set in startup (Cordova side):

CordovaPush.push.on('notification', data => {
      console.log('this is my message: ', data)
    })

Results in this object: Screen Shot 2019-11-05 at 00 16 13

I hope this answers your query, let me know otherwise. I'd also be interested in some feedback. This you find it easy to have this setup? Is the documentation clear?

jamauro commented 4 years ago

Thanks for pointing me in the right direction. I'll give that a go.

The setup was more or less straightforward. I wish I had written down the areas where I got a little stuck. One thing that I had to do when upgrading was remove the package and then add it back. For some reason meteor update didn't grab the latest.

Thanks for adding this one to the documentation!

jamauro commented 4 years ago

@paulincai for some reason I can't get the console to display anything with this:

CordovaPush.push.on('notification', data => {
      console.log('this is my message: ', data)
    })

I took a look at the phonegap-plugin-push documentation and they mention that 'content-available': '1' must be added in order for the push.on('notification') handler to trigger. I tried adding it to the notification that's sent with Push.send like this:

// server side

const pushNotification = {
        title: title,
        body: body,
        badge: badge,
        userId: userId,
        notId: Math.round(new Date().getTime() / 1000),
        data: { path: urlPath },
        'content-available': '1'
      }

     Push.send(pushNotification)

But this seems to prevent the push notification from being sent at all. I'm not seeing any client or server-side errors.

Is there something else that I'm missing?

paulincai commented 4 years ago

Did you set this in the same startup file where you initialize Push on the client? This is a listener and would need to be started on Meteor startup (after Cordova is loaded). Pfff, right now I can’t remember whether I did the screen shot with IOS or Android, perhaps that is the Safari debugging and not Chrome which means I must have tested it on IOS ... in case this info is of any use.

jamauro commented 4 years ago

Yes, your screenshot is Safari's console so it looks like you got it working on iOS :).

Here's what I have in client/startup.js


import { CordovaPush } from 'meteor/activitree:push'

Meteor.startup(function() {
  if (Meteor.isCordova) {
    CordovaPush.Configure({
      appName: 'appname',
      debug: true,
      ios: {
        alert: true,
        badge: true,
        sound: true,
        clearBadge: true,
        topic: 'com.xxxx.xxxx' // your IOS app id.
      }
    });

    CordovaPush.push.on('notification', data => {
      console.log("notification: ", data);
    }
});