简体   繁体   中英

Sending individual push notifications with FCM

I have a simple WebView application of a mobile responsive website with authentication feature(the authentication is not in the app itself but in the WebView -> it's using sessions and cookies for long-term login)

What I want to do is send push notifications individually for each user based on the data in the database of the website.

My problems are the following:

  1. How do I make the 'connection' between the logged in user in the WebView and the app?(Perhaps extract the login cookie from the WebView?)

  2. How do I use FCM specifically for individual users(the one's gathered and associated at question 1) and not general notifications. I've found some info on this that I'd have to create a 'topic' for each user which doesn't sound too reliable, is this how FCM is used for individual notifications?

The function looks like this(node.js):

function sendNotificationToUser(username, message, onSuccess) {
  request({
    url: 'https://fcm.googleapis.com/fcm/send',
    method: 'POST',
    headers: {
      'Content-Type' :' application/json',
      'Authorization': 'key='+API_KEY
    },
    body: JSON.stringify({
      notification: {
        title: message
      },
      to : '/topics/user_'+username
    })
  }, function(error, response, body) {
    if (error) { console.error(error); }
    else if (response.statusCode >= 400) { 
      console.error('HTTP Error: '+response.statusCode+' - '+response.statusMessage); 
    }
    else {
      onSuccess();
    }
  });
}

source: https://firebase.googleblog.com/2016/08/sending-notifications-between-android.html

Aside from topic , notifications can also be sent using a device-specific Notification Token . These notification tokens are device-specific and to my knowledge, outside of the auth system. If I understand correctly, your app depends on the WebView for all purposes of the app.

What I would suggest is to follow the aforementioned link, and implement the token retrieval in your app, then pass it along as a URL parameter. Instead of topic , you can pass a list(or single) notification token(s) to target individual devices(not users).

I hope this helps!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM