Retrieve device token from device using ionic framework

后端 未结 3 1613
故里飘歌
故里飘歌 2020-12-05 22:05

I am using ionic to develop an application and I need to get the device token for push notifications, but having trouble. The application isn\'t receiving noti

相关标签:
3条回答
  • 2020-12-05 22:08

    Here is the ng-Cordova document which is for getting current device token id.

    copy and past it in your project controller.

    In the given below line you can find notification parameter

    $rootScope.$on('$cordovaPush:notificationReceived', function(event, notification) 
    

    The notification is an object and it will have an regid field which provides you the current mobile device id you can make use of that like (posting to server, view in console.log(notification);)

    To send the device token to you server you need just do

    var loginPost = {
                        "UserName":"Mike",
                        "PassWord":"xxxxxx",
                        "DeviceID":notification.regid
                      };
    

    Using this loginPost variable object post to the server.

    0 讨论(0)
  • 2020-12-05 22:21

    use the below plugin to get the device token value. ionic plugin add phonegap-plugin-push --variable SENDER_ID="GCM_PROJECT_NUMBER"

    please use the below code to get the device token value.

    .run(function($ionicPlatform) {
        $ionicPlatform.ready(function() {
            push = PushNotification.init({
                android: {
                    senderID: "61426985247",
                    sound: "true",
                    vibrate: "true",
                    forceShow: "true"
                },
                browser: {
                    pushServiceURL: 'http://push.api.phonegap.com/v1/push'
                },
                ios: {
                    alert: "true",
                    badge: true,
                    sound: 'false'
                },
                windows: {}
            });
    
    
    
            push.on('registration', function(data) {
    
                alert("device token" + data.registrationId);
    
            });
        });
    })
    
    0 讨论(0)
  • 2020-12-05 22:31

    I've used phonegap-plugin-push plugin and its pretty easy and simple. For regID in the code on deviceReady event I've used.

    var push = PushNotification.init({
            "android": {
                "senderID": "SENDER-ID"
            },
            "ios": {"alert": "true", "badge": "true", "sound": "true"}, 
            "windows": {} 
        });
    
        push.on('registration', function(data) {
            console.log("registration event");
            //here is your registration id
            console.log(data.registrationId);
        });
    

    This is the link of the tutorial too

    Hope it helps.

    0 讨论(0)
提交回复
热议问题