如何在Cordova平台中实现推送通知?

时间:2022-09-02 11:08:23

I am trying to implement the push notifications concept in my app. After successful installation of plugin, push.on(registration) method is not calling

我正在尝试在我的应用程序中实现推送通知概念。成功安装插件后,push.on(注册)方法不会调用

My Project structure is projectname/platforms/android/assets/www

我的项目结构是projectname / platforms / android / assets / www

In www folder contains all html,js,css files

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

notification.js file, I have called in homepage.html

notification.js文件,我在homepage.html中调用过

I have written the code in notification.js is:

我在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");

            });

}

Here, push.on(registration) and push.on(notification) the method is not calling please let us know the possible ways to get the notification for a specific device

这里,push.on(注册)和push.on(通知)方法没有调用请告诉我们获取特定设备通知的可能方法

1 个解决方案

#1


2  

After successful installation of push notification plugin, i have received the notification in Android

成功安装推送通知插件后,我已收到Android中的通知

Process:

处理:

By using the below link i have installed the push-notification plugin

通过使用以下链接,我已经安装了推送通知插件


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

cordova插件添加https://github.com/phonegap/phonegap-plugin-push --variable SENDER_ID =“xxxxxxxxxxxxxx”

Installation Requirements:

安装要求:

-Android version > 6.0.0
-iOS Version > 4.3.0 better

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

For iOS Version, pods required.so we need to install pod

对于iOS版本,需要pods。所以我们需要安装pod

sudo gem install cocoapods

For GCM Registration: https://developers.google.com/mobile/add

对于GCM注册:https://developers.google.com/mobile/add

After succesfull installation, pod file will be created . Once it done the open the project.xcworkspace file. Then ios app will run properly

成功安装后,将创建pod文件。完成后打开project.xcworkspace文件。然后ios应用程序将正常运行

If you are calling the notification in middle of the app, then write the addEventListener method

如果您在应用程序中间调用通知,则编写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");

                });

    }

#1


2  

After successful installation of push notification plugin, i have received the notification in Android

成功安装推送通知插件后,我已收到Android中的通知

Process:

处理:

By using the below link i have installed the push-notification plugin

通过使用以下链接,我已经安装了推送通知插件


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

cordova插件添加https://github.com/phonegap/phonegap-plugin-push --variable SENDER_ID =“xxxxxxxxxxxxxx”

Installation Requirements:

安装要求:

-Android version > 6.0.0
-iOS Version > 4.3.0 better

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

For iOS Version, pods required.so we need to install pod

对于iOS版本,需要pods。所以我们需要安装pod

sudo gem install cocoapods

For GCM Registration: https://developers.google.com/mobile/add

对于GCM注册:https://developers.google.com/mobile/add

After succesfull installation, pod file will be created . Once it done the open the project.xcworkspace file. Then ios app will run properly

成功安装后,将创建pod文件。完成后打开project.xcworkspace文件。然后ios应用程序将正常运行

If you are calling the notification in middle of the app, then write the addEventListener method

如果您在应用程序中间调用通知,则编写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");

                });

    }