向android设备发送通知。已发送但未收到

时间:2015-11-10 19:10:31

标签: javascript android cordova google-cloud-messaging pubnub

我熟悉向设备发送通知,

我目前正在尝试使用pubnub和google云消息传递给我的设备:

(function() {



    var pubnub = PUBNUB.init({
        subscribe_key: 'my-subscribe',
        publish_key:   'my-publish',
    });

    var os = 'android';
    var regid = '';
    var channel = 'mi-canal';




    function sendPush() {
        var gwtype = (os === 'ios') ? 'apns' : 'gcm';
        console.log('sending push notification...',channel,gwtype);



        pubnub.mobile_gw_provision({
            device_id: 'APA91bE0rX8mfzY0VttACffUPIagSbd5No0xxxxxxxxxxxxxxxxxEHCVFBbzPqPHKG9phyQ31O4W6aEgMUqWANtWrK-zdMsiONUwAbUW4efso8ScemlXGM8tJq0M12oR0E2Kk9',
            channel: channel, 
            op: 'add', 
            gw_type: gwtype,
            error: function(msg){console.log(msg);},
            callback: function() {
                var message = PNmessage();

                message.pubnub = pubnub;
                message.callback = function (msg){ console.log(msg); };
                message.error = function (msg){ console.log(msg); };
                message.channel = channel;
                message.apns = {
                    alert: 'The room temperature is set too high'
                };
                message.gcm = {
                    title: 'PubNub Push Demo',
                    message: 'The room temperature is set too high'
                };

                message.publish();
            }            
        });

    }
    sendPush();
})();

Wich logs:

[1, "Sent", "14471821928645156"]

问题是我没有收到设备中的通知,用和android app(cordova制作)

var pushNotification = window.plugins.pushNotification;


            //pushNotification.unregister(successHandler, errorHandler);
            pushNotification.register(
                successHandler, 
                errorHandler, 
                {
                    'senderID':'api-project-xxxxxxxx',
                    'ecb':'onNotificationGCM' // callback function
                }
            );  
            pusNotification.subscribe({
                channel: 'mi-canal',
                message: function(m){
                    alert('Conslog: '+m);
                },
            error: function (error) {
              // Handle error here
              console.log(JSON.stringify(error));
            }
            });

知道我缺少什么吗?

2 个答案:

答案 0 :(得分:2)

我个人在我的Android应用程序上使用OneSignal,它就像一个魅力。 您可以阅读文档here。它是100%免费的,也使用谷歌的云消息服务。基本上在安装他们的SDK并遵循整个文档后,您的代码将是这样的。

代码示例:

document.addEventListener('deviceready', function () {
  // Enable to debug issues.
  // window.plugins.OneSignal.setLogLevel({logLevel: 4, visualLevel: 4});

  var notificationOpenedCallback = function(jsonData) {
    console.log('didReceiveRemoteNotificationCallBack: ' + JSON.stringify(jsonData));
  };

  window.plugins.OneSignal.init("b2f7f966-d8cc-11e4-bed1-df8f05be55ba",
                                 {googleProjectNumber: "703322744261"},
                                 notificationOpenedCallback);

  // Show an alert box if a notification comes in when the user is in your app.
  window.plugins.OneSignal.enableInAppAlertNotification(true);
}, false);
  
      
  • 替换" b2f7f966-d8cc-11e4-bed1-df8f05be55ba"使用您的OneSignal应用程序ID。
  •   
  • 替换" 703322744261"使用您的Google项目编号。
  •   

对我来说,只要我发送通知,就需要3秒才能发出哔哔声。希望这会有所帮助:)

答案 1 :(得分:1)

Android上的PubNub Cordova HTML5

  

Sending Android Push Notifications via GCM in JavaScript

嗨托尼!我看到您正在使用CordovaAndroid为您HTML5应用。您需要等待PubNub网络系统为GCM分配和配置设备。我认为这可能是个问题。只需在发送GCM有效负载之前添加延迟。另外,我添加了一个指向今日使用Cordova / PhoneGap 在Android上开始 GCM推送通知的最佳指南的链接。

Cordova GCM推送通知

您可能想知道是否可以从使用JavaScript编写的网络应用发送Android推送通知而无需设置您自己的服务器。好吧,当你将PubNub Push API与PhoneGap结合使用时,绝对是这样!

GCM Cordova PhoneGap

调试推送通知

您可以使用频道的-pndebug频道进行一些高级调试,并检查设备是否仍然在频道中注册。

pndebug频道

  1. 使用PubNub Dev Console订阅频道的-pndebug频道。如果您要发布的频道为foo,请订阅foo-pndebug。确保使用正确的pub / sub键。
  2. 'pn_debug':true添加到顶级消息有效内容并发布该消息。

    "pn_gcm": {
        "data": {
            "message": "hello"
        },
    }
    "pn_apns": {
        "aps": {
            "alert": "hello"
        },
    }
    "pn_debug": true
    
  3. 将消息发布到频道foo。如果推送消息注册有任何问题,您应该在订阅foo-pndebug的开发者控制台中看到有用的错误消息。

  4. 检查频道注册

    在浏览器中使用简单的REST URL发布推送通知后,您还可以检查设备是否仍注册到频道。

    http://pubsub.pubnub.com/v1/push/sub-key/<your-sub-key>/devices/<device-reg-token>?type=gcm

    这将返回设备仍然注册的所有频道以进行推送通知(在这种情况下为GCM。根据需要将type查询参数更改为apns

    返回:["demo", "demo1", "demo2"]

    如果您刚刚发送推送通知的频道不在该列表中,则可能存在设备注册令牌问题(例如,无效令牌或未注册的设备)。但是-pndebug频道应该向您透露这一点。