简体   繁体   English

使用推送通知时如何获取用户设备令牌

[英]how to get the users device token when using push-notification

I am building an app that sends push notifications to IOS and android phones and on searching i found "push-notification" it allows Cross-platform Push Notifications my problem is how do we get the iosToken and the androidToken for the users devices? 我正在构建一个向iOS和Android手机发送推送通知的应用程序,在搜索时我发现“推送通知”它允许跨平台推送通知,我的问题是我们如何为用户设备获取iosToken和androidToken? If anyone has used this before i would appreciate a push in the right direction. 如果有人使用过此功能,我将感激朝正确的方向发展。 Thanks in advance and you can find my code below. 预先感谢,您可以在下面找到我的代码。

var PushNotification = require('push-notification');
var DeviceType = PushNotification.DeviceType;
var path = require('path');

// APN: cert.pem, key.pem should be configured
// GCM: configure console to generate gcm.sender
PushNotification.init({
    apn: {
    cert: path.resolve('./keys/cert.pem'),
    key: path.resolve('./keys/key.pem')
},
gcm: {
    apiKey: 'gcm-api-key'
}
});

var iosToken = 'iphone-device-token';
var androidToken = 'android-device-token';
var message = 'some text to push...';
var badge = null;
var sound = null;
var payload = null;

// send a notification to a single device
PushNotification.pushSingle(DeviceType.IOS, iosToken, message, badge,    sound, payload);
PushNotification.pushSingle(DeviceType.ANDROID, androidToken, message, badge, sound, payload);

Device token is assigned by Google to particular device which will register for play services. Google将设备令牌分配给要注册用于播放服务的特定设备。

For example if you are registering for GCM, on successful registration device will get registration key. 例如,如果您正在注册GCM,则在成功注册的设备上将获得注册密钥。

This key should be passed onto server so that you can push message to device. 该密钥应传递到服务器上,以便您可以将消息推送到设备。 Please note we need individual device token. 请注意,我们需要单独的设备令牌。 For mass broadcast we are looping over keys and pushing message. 对于大规模广播,我们将遍历按键并推送消息。 Please post here if you find any other way. 如果您找到其他方法,请在此处发布。

When the device token is generated inside the application of the user's device, the device token could be pushed and stored into a database that your push notification server has access to. 当在用户设备的应用程序内部生成设备令牌时,可以将设备令牌推送并存储到您的推送通知服务器可以访问的数据库中。 This could be done via a REST API. 这可以通过REST API完成。

Depending on your needs, a table within your database consisting of users could contain a device token for his iPhone and Android phone that is unique to the user. 根据您的需求,数据库中由用户组成的表可以包含该用户唯一的iPhone和Android手机的设备令牌。

So whenever you need to broadcast a notification to a specific user you would look up the user's device token in the database and build the notification from there. 因此,每当您需要向特定用户广播通知时,您都将在数据库中查找该用户的设备令牌并从那里构建通知。

I did something similar in a recent project I was involved in. I hope it helps. 在最近参与的一个项目中,我做了类似的事情。希望对您有所帮助。

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

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