1

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);
user3659384
  • 37
  • 2
  • 8
  • Device token can only be generated by the devices. So you have to make an api which will get you the device token from devices. – Pankaj Sep 02 '15 at 05:34
  • Try to read this tutorial it for the cross-platform but if you dive in it there are all links to gsm and apns cases http://devgirl.org/2012/10/25/tutorial-android-push-notifications-with-phonegap/ – Ignat Galkin Sep 02 '15 at 06:06

2 Answers2

2

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.

Prince John
  • 642
  • 1
  • 4
  • 17
  • How to generate those tokens? – Becario Senior Mar 07 '17 at 21:30
  • @BecarioSenior it usually depends on the platform you want to generate the device token for. You can refer to these questions for Android and iOS. http://stackoverflow.com/questions/33560598/how-can-we-get-device-token-for-android-for-push-notification http://stackoverflow.com/questions/8798725/get-device-token-for-push-notification – Prince John Mar 08 '17 at 08:07
0

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.

Isham
  • 424
  • 4
  • 14