简体   繁体   English

android手机间隙中的推送通知未收到通知

[英]android push notification in phone gap not getting notifications

Hi I follow the steps below 嗨,我按照以下步骤

1) I created the cordova project structure. 1)我创建了cordova项目结构。
2) I added the platform( android). 2)我添加了平台(android)。
3) I added the cordova plugin 3)我添加了cordova插件

cordova plugin add https://github.com/phonegap-build/PushPlugin.git#2.4.0

4) Bulid the cordova project. 4)编译cordova项目。
5) Next I import the created app in android eclipse(4.4.2) 5)接下来,我将创建的应用程序导入android eclipse(4.4.2)
6) I wrote the code below in index.js file 6)我在index.js文件中编写了以下代码

 init: function(){
     alert("init");
    var pushNotification = window.plugins.pushNotification;

    pushNotification.register(successHandler, errorHandler, 
        {
            'senderID':'XXXXXXXXXX',
            'ecb':'onNotificationGCM' // callback function
        }
    );

    function successHandler(result) {
        console.log('Success: '+ result);
        alert(result);
    }

    function errorHandler(error) {
        console.log('Error: '+ error);
    }

     function onNotificationGCM(e) {
        alert("comming");
        if('registered' === e.event) {
            // Successfully registered device.
        }
        else if('error' === e.event) {
            // Failed to register device.
        }
    };

I am getting the respose as "OK".and i am not able call 'ecb': onNotificationGCM' // callback function 我得到的respose为“ OK”。我无法调用“ ecb”:onNotificationGCM'//回调函数

In Android console I am getting the bellow Message 在Android控制台中,我收到以下消息

V/PushPlugin(2512): execute: action=register 
V/PushPlugin(2512): execute: data=     [{"senderID":"889953963751","ecb":"onNotificationGCM"}] V/PushPlugin(2512): execute: jo={"senderID":"889953963751","ecb":"onNotificationGCM"} V/PushPlugin(2512): execute: ECB=onNotificationGCM senderID=889953963751
    09-12 03:13:33.453: D/GCMRegistrar(2512): resetting backoff for com.ensis.hello
    09-12 03:13:33.613: V/GCMRegistrar(2512): Registering app com.ensis.hello of senders 889953963751
    09-12  W/PluginManager(2512): THREAD WARNING: exec() call to PushPlugin.register blocked the main thread for 181ms. Plugin should use CordovaInterface.getThreadPool().

This is the push notification flow: 这是推送通知流程:

  1. your app request a registration to the remote Apple or Google server 您的应用程序请求注册到远程Apple或Google服务器
  2. if the registration is ok, the remove server returns a token, that identify this specific app on your device 如果注册正常,则删除服务器将返回一个令牌,该令牌标识您设备上的该特定应用
  3. you send this token to your server, saving it (eg on a db) 您将此令牌发送到您的服务器,将其保存(例如在数据库上)
  4. you send a push notification (from your server) calling Apple or Google services with the message and the tokens of the users you want to notify 您发送(从您的服务器)调用Apple或Google服务的推送通知,其中包含消息和您要通知的用户令牌
  5. these services push to your device/app a notification with the message 这些服务会向您的设备/应用推送一条消息通知

You must follow all these steps in order to have push notification working. 您必须执行所有这些步骤才能使推送通知正常工作。

For android you need to catch the registration id (token) inside the registered event of the notification handler: 对于android,您需要在通知处理程序的registered事件内捕获注册ID(令牌):

function onNotificationGCM(e) {
    alert("comming");
    if('registered' === e.event) {
        // Successfully registered device.
        console.log("regID = " + e.regid);
        // save/send this registration id on your server
    } else if('error' === e.event) {
        // Failed to register device.
    }
};

For iOS you need to catch it in the succesHandler of the register function. 对于iOS,您需要在register函数的succesHandler中捕获它。

For more information look at this example in the plugin repository . 有关更多信息,请参阅插件存储库中的此示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM