【问题标题】:How to get cordova android push notifications, when the app is in idle state or is in background?当应用程序处于空闲状态或处于后台时,如何获取cordova android推送通知?
【发布时间】:2015-08-31 19:38:08
【问题描述】:

我们如何才能在 phonegap android 应用程序中获得 GCM 推送通知,即使应用程序处于空闲状态或处于后台也是如此。

“katzer/cordova-plugin-background-mode”似乎不起作用...

当应用程序在前台运行时,我正在成功收到推送通知

cordova 版本:4.3.0 安卓4.4 phonegap 4.2.0

我将在下面复制我的通知功能... 设备就绪

函数 onDeviceReady() {

        try 
            {
                      pushNotification = window.plugins.pushNotification;
              jQuery("#app-status-ul").append('<li>registering ' + device.platform + '</li>');
                      if (device.platform == 'android' || device.platform == 'Android' ||
                                device.platform == 'amazon-fireos' ) {
            pushNotification.register(successHandler, errorHandler, {"senderID":"my-project-id","ecb":"onNotification"});   
              } else {
                          pushNotification.register(tokenHandler, errorHandler, {"badge":"true","sound":"true","alert":"true","ecb":"onNotificationAPN"});  // required!
                      }
            }
        catch(err) 
            { 
              txt="There was an error on this page.\n\n"; 
              txt+="Error description: " + err.message + "\n\n"; 
              alert(txt); 
            } 
        }

函数 onNotification(e) {

            switch( e.event )
            {
                case 'registered':


      if ( e.regid!='' )
      {

           android_reg_id =  e.regid;


          jQuery.ajax({
            type:"POST",
            url:SITEURL+"index.php?r=Manageuser/App_Reg_Android",
            data:{regid: android_reg_id,employeeno:employeeno}
        }).done(function(msg) {

        });             

      }
                break;                    
                case 'message':
                  // if this flag is set, this notification happened while we were in the foreground.
                  // you might want to play a sound to get the user's attention, throw up a dialog, etc.
                  if (e.foreground)
                  {                      
               //jQuery("#app-status-ul").html('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');
                // on Android soundname is outside the payload. 
                      // On Amazon FireOS all custom attributes are contained within payload
                      var soundfile = e.soundname || e.payload.sound;
                      // if the notification contains a soundname, play it.
                      // playing a sound also requires the org.apache.cordova.media plugin
                      var my_media = new Media("/www/"+ soundfile);

                      my_media.play();
                   }
        else
        {


               if (e.coldstart)
                    $("#app-status-ul").append('<li>--COLDSTART NOTIFICATION--' + '</li>');
               else
                    $("#app-status-ul").append('<li>--BACKGROUND NOTIFICATION--' + '</li>'); 
                    //location.href = e.payload.redid;
        // otherwise we were launched because the user touched a notification in the notification tray.

        }

          jQuery("#app-status-ul").html('<li>MESSAGE -> MSG: ' + e.payload.message + '</li>');        
        //window.localStorage.setItem("push_que", e.payload.redid);
        //location.href = e.payload.redid;

        break;

                case 'error':
        jQuery("#app-status-ul").append('<li>ERROR -> MSG:' + e.msg + '</li>');
                break;

                default:
        jQuery("#app-status-ul").append('<li>EVENT -> Unknown, an event was received and we do not know what it is</li>');
                break;
            }
        }

使用的插件是 com.phonegap.plugins.PushPlugin 2.4.0 "PushPlugin"

【问题讨论】:

  • 推送通知没有问题,但是当应用程序空闲时它不起作用.....
  • 在后台运行 Cordova 应用程序是它所需要的。但不知道怎么做!
  • 我不知道你是否解决了你的问题,但我有一个类似的。事实证明,除非通知具有“消息”属性,否则我的科尔多瓦插件不会在后台接收推送通知。也许这与您遇到的问题相同。这是我的question

标签: android cordova


【解决方案1】:

接收推送通知不需要应用在后台运行。

Cordova 推送插件
我推荐这个插件:https://github.com/phonegap-build/PushPlugin 用于接收推送通知(当应用程序未运行时)。
该页面上的文档非常好。
该插件支持iOS、Android等平台。

服务器端选项
我不知道您在服务器上运行什么,实际上听起来您在服务器端不一定有任何问题,但为了提供更完整(和更通用)的科尔多瓦推送解决方案答案,我想提一下:
对于 Android GCM 推送消息,请查看 https://www.npmjs.com/package/node-gcm
对于 iOS APN 推送消息,有 https://github.com/argon/node-apn
我包含这些链接是因为,即使您没有在服务器上运行节点,这些页面上的文档也是一个很好的起点,它们都有指向更多真正有用信息的链接。

----- 2015 年 4 月 12 日更新 -----

上面推荐的插件已被弃用,替换插件 https://github.com/phonegap/phonegap-plugin-push 具有更简单的界面。
这个简单的客户端代码示例包含大多数用例所需的一切:https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/EXAMPLES.md

【讨论】:

    【解决方案2】:

    我不确定您使用哪个插件来捕获推送通知,但我建议您使用phonegap-build/PushPlugin。它有一个处理程序,可以在应用程序打开、后台或关闭时捕获通知。如果您按下通知,它将打开您的应用程序。

    要使用该插件,只需将其放入您的代码中:

    var pushNotification;
    
    document.addEventListener("deviceready", function(){
        pushNotification = window.plugins.pushNotification;
        if ( device.platform == 'android' || device.platform == 'Android' ){
            pushNotification.register(
            successHandler,
            errorHandler,
            {
                "senderID":"replace_with_sender_id",
                "ecb":"onNotification"
            });
        }
        //the rest of your deviceReady function
    });
    
    // result contains any message sent from the plugin call
    function successHandler (result) {
        alert('result = ' + result);
    }
    
    // result contains any error description text returned from the plugin call
    function errorHandler (error) {
        alert('error = ' + error);
    }
    

    现在我们已经为全局变量pushNotification 设置了一个插件实例,并设置了一个 if 语句,用于向 GCM 服务注册您的 android 设备,但您需要将您的 Google API 项目的 senderID 放在这里:"senderID":"replace_with_sender_id" . 如果设备注册成功,它将调用函数onNotification。 该函数应该执行以下操作:

    function onNotification(e) {
        console.log('event received: '+e.event);
    
        switch( e.event )
        {
        case 'registered':
            if ( e.regid.length > 0 )
            {
                //Here you should call a function that sends the registration-ID
                //to your server so you can save it and send push notifications to your device
                console.log("regID = " + e.regid);
            }
        break;
    
        case 'message':
            if ( e.foreground )
            {
                //App was open when the notification was received
                console.log('foreground');
                // on Android soundname is outside the payload.
                var soundfile = e.soundname || e.payload.sound;
                // if the notification contains a soundname, play it.
                var my_media = new Media("/android_asset/www/"+ soundfile);
                my_media.play();
            }
            else
            {   
                if ( e.coldstart )
                {
                    //App was closed
                    console.log('coldstart');
                }
                else
                {
                    //App was open in the background
                    console.log('background');
                }
            }
           alert('message: '+e.payload.message);
        break;
    
        case 'error':
            alert('GCM error: '+e.msg);
        break;
    
        default:
            alert('An unknown event has occurred');
        break;
      }
    }
    

    这个函数从 GCM 服务接收一个事件,告诉它做什么。如果设备已注册,它会在控制台中记录设备注册 ID,如果收到消息,它会检查应用程序是否在后台打开、关闭或打开,它会提醒消息 alert('message: '+e.payload.message);

    由于您使用的是 Android,我刚刚包含了 Android 的代码。 我希望这就是你要找的。​​p>

    【讨论】:

      猜你喜欢
      • 2022-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多