javascript - 向 Android 设备发送通知。已发送但未收到

标签 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();
})();

哪些日志:

[1, "已发送", "14471821928645156"]

问题是我在设备中没有收到 Android 应用程序(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));
            }
            });

知道我错过了什么吗?

最佳答案

我个人使用OneSignal在我的 Android 应用程序上,它就像一个魅力。 您可以阅读文档here 。它是 100% 免费的,并且还使用 Google 的云消息服务。基本上在安装了他们的 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);
  • Replace "b2f7f966-d8cc-11e4-bed1-df8f05be55ba" with your OneSignal App Id.
  • Replace "703322744261" with your Google Project Number.

对我来说,当我发送通知后,我的手机会在 3 秒内发出蜂鸣声。希望这有帮助:)

关于javascript - 向 Android 设备发送通知。已发送但未收到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33637685/

相关文章:

javascript - 这个 javascript 对象修改是如何工作的?我不明白范围,它不会持续存在

android - 第三方 Intent 的可浏览数据库?

javascript - 有什么方法可以使用 MutationObserver 来监视带有指定选择器的元素的插入?

android - 尝试将不透明图像附加到非不透明 ImageWriter 中,反之亦然

java - 通过 cellId、LAC、MCC 和 MNC Android 获取当前移动站与相邻基站的距离和方向?

Android 将手机数据写入屏幕

ios - 静音 Cordova 插件 cordova-plugin-camera-preview

javascript - phonegap iPhone 应用程序缓存旧数据

javascript - 如何检测 iframe 是否已从一个 URL 移动到另一个 URL

javascript - 如何使用 Vue.js 将用户登录到 spring-security 应用程序?