简体   繁体   English

使用 FCM 发送单独的推送通知

[英]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)我有一个简单的 WebView 应用程序,它是一个具有身份验证功能的移动响应网站(身份验证不在应用程序本身,而是在 WebView -> 它使用会话和 cookies 进行长期登录)

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?)如何在 WebView 中的登录用户和应用程序之间建立“连接”?(也许从 WebView 中提取登录 cookie?)

  2. How do I use FCM specifically for individual users(the one's gathered and associated at question 1) and not general notifications.我如何专门为个人用户使用 FCM(在问题 1 中收集并关联)而不是一般通知。 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?我发现了一些关于此的信息,我必须为每个用户创建一个听起来不太可靠的“主题”,这就是 FCM 用于单个通知的方式吗?

The function looks like this(node.js): function 看起来像这样(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来源: 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 .除了topic ,还可以使用特定于设备的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.如果我理解正确,您的应用程序的所有用途都依赖于WebView

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.我建议按照上述链接,在您的应用程序中实现令牌检索,然后将其作为 URL 参数传递。 Instead of topic , you can pass a list(or single) notification token(s) to target individual devices(not users).除了topic ,您可以传递一个列表(或单个)通知令牌来定位单个设备(而不是用户)。

I hope this helps!!我希望这有帮助!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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