简体   繁体   中英

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? 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.

For example if you are registering for GCM, on successful registration device will get registration key.

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.

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.

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.

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