簡體   English   中英

如何在Cordova平台中實現推送通知?

[英]How to implement push notifications in Cordova Platform?

我正在嘗試在我的應用程序中實現推送通知概念。 成功安裝插件后, push.on(registration)方法未調用

我的項目結構是projectname/platforms/android/assets/www

www文件夾中包含所有html,js,css文件

notification.js文件,我已經在homepage.html調用了

我在notification.js中編寫的代碼是:

document.addEventListener('deviceready', pushNotification, false);
function PushNotification(){
var push = PushNotification.init({ "android": {"senderID": "GCMProjectId(123456789)"},"ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {} } );
       push.on('registration', function(data) {
            alert("registration id is:"+data.registrationId);
            var id = localStorage.getItem("userId");
            var notifyInput = {
                        "token":data.registrationId,
                        "type":"android",
                        "uid":id
               }
            });

    push.on('notification', function(data) {
              alert(data.message);
            });

    push.on('error', function(e) {
            // e.message
            alert("error function calling on push notifications");

            });

}

在這里, push.on(registration) and push.on(notification)方法未調用,請讓我們知道獲取特定設備通知的可能方法

成功安裝推送通知插件后,我已經在Android中收到通知

處理:

通過使用下面的鏈接,我已經安裝了推送通知插件

cordova plugin add https://github.com/phonegap/phonegap-plugin-push --variable SENDER_ID="xxxxxxxxxxxxxx"

安裝要求:

-Android版本> 6.0.0
-iOS版本> 4.3.0更好

對於iOS版本,需要安裝Pod。因此,我們需要安裝Pod

sudo gem install cocoapods

對於GCM注冊: https : //developers.google.com/mobile/add

成功安裝后,將創建pod文件。 完成后,打開project.xcworkspace文件。 然后ios應用將正常運行

如果您在應用程序的中間調用通知,請編寫addEventListener方法

document.addEventListener('deviceready', pushNotification, false);

    function PushNotification(){
    var push = PushNotification.init({ "android": {"senderID": "xxxxxx(refers project number in GCM)"},"ios": {"alert": "true", "badge": "true", "sound": "true"}, "windows": {} } );

           push.on('registration', function(data) {
                alert("registration id is:"+data.registrationId);
               // registration id need to pass your notification server

                });

        push.on('notification', function(data) {
                  alert(data.message);
                  // you receive the notification
                });

        push.on('error', function(e) {
                // e.message
                alert("error function calling on push notifications");

                });

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM