【问题标题】:Cordova Android Push Notification With Action Button带有操作按钮的 Cordova Android 推送通知
【发布时间】:2016-08-18 12:18:03
【问题描述】:

我使用 Push Plugin ,当我发送带有操作按钮 1) 接受 2) 忽略的推送时。

当通知到来时,我被点击了“接受”按钮。但我想要带有“接受”按钮回调的参数。从此我将识别通知的“接受”被调用。

代码参考

//initialization of push object
        var push = PushNotification.init({
            "android": {
                "alert": "true",
                "senderID": CONFIG.PROJECT_NUMBER,
                "icon": "img/ionic.png",
                "iconColor": "blue",
                "badge": "true"
            },
            "ios": {
                "alert": "true",
                "badge": "true",
                "sound": "true"
            }, 
            "windows": {

            } 
        });

        //listner for getting registration detail of device
        push.on('registration', function(data) {
            device_id_for_push=data.registrationId;
        });

        //listner called on new push notification
        push.on('notification', function(data) {
            // app.onPushAccept(data);
            alert("on notification");
            alert(JSON.stringify(data));
        });

        //error listner
        push.on('error', function(e) {
            // alert(e);
            // alert("push error");
        });

        app.onPushAccept=function(data){
            alert("onPushAccept")
            alert(JSON.stringify(data));
            // cordova.plugins.notification.badge.clear();
            // cordova.plugins.notification.badge.increase();
        }

代码中的“app.onPushAccept”函数是“接受”按钮的回调..

请尽快帮助我。 谢谢你。。

【问题讨论】:

    标签: android cordova push-notification


    【解决方案1】:

    好的,首先假设您正在发送以下有效负载:

    {
        "registration_ids": ["my device id"],
        "data": {
            "title": "My title",
            "message": "My message.",
            "actions": [
                { "title": "Accept", "callback": "app.accept", "foreground": true},
                { "title": "Ignore", "callback": "app.ignore", "foreground": false}
            ]
        }
    }
    

    并且您已经设置了以下操作按钮处理程序:

    app.accept = function(data} {
      // do something
    }
    
    app.ignore = function(data} {
      // do something
    }
    

    所以现在您有两个选择,您可以在推送有效负载中放入唯一标识接收到的推送的内容,该推送将放入 data.additionalData 或修改回调以调用另一个事件处理程序:

    app.accept = function(data} {
      app.processPush('accept', data);
    }
    
    app.ignore = function(data} {
      app.processPush('ignore', data);
    }
    
    app.processPush = function(type, data) {
      if (type === 'accept') {
        // do accept stuff
      } else if (type === 'ignore') {
        // do ignore stuff
      }
    }
    

    【讨论】:

      【解决方案2】:

      Android 推送通知(仅限)

      第 1 步 - 首先转到以下目录

           plugins > phonegap-plugin-push > src > android > com > adobe > phonegap > push
      

      第 2 步 - 从上面的目录打开 GCMIntentService.java 文件

      第 3 步 - 确定调用“createActions”的函数和 添加实际参数“requestCode”,如...

           createActions(extras,mBuilder,resources,packageName,notId,requestCode);
      

      第 4 步 - 确定函数定义“createActions”和 添加形参“int requestCode”,如...

           private void createActions(Bundle extras, NotificationCompat.Builder mBuilder, Resources resources, String packageName, int notId,int requestCode)
      

      第 5 步 - 在函数定义“createActions”和 for 循环中 将第二个参数从“i”更改为“requestCode”,如...

           pIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      
           pIntent = PendingIntent.getBroadcast(this, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
      

      第 6 步 - 完成上述所有步骤后,如果已添加平台,请删除 android 平台,然后添加 android 平台。

      如果在我的解决方案中发现任何错误,请见谅并改进。

      【讨论】:

      • 如果我们使用 ionic,有没有办法实现这样的事情?我不想为了实现这个而干预插件文件夹。
      • 说是因为这个文件夹是在构建apk的过程中生成的,以防离子。换句话说,我可以通过更改 config.xml 和/或 package.json 中的某些内容(或添加)来实现此目的吗?
      【解决方案3】:

      使用插件cordova-plugin-dialogsnavigator.notification.confirm方法

      它会显示一个可自定义的确认对话框。

      语法

      navigator.notification.confirm(message, confirmCallback, [title], [buttonLabels])
      
      • message:对话框消息。 (字符串)
      • confirmCallback:在按下按钮的索引(1、2 或 3)或在没有按下按钮(0)的情况下关闭对话框时调用的回调。 (函数)
      • title:对话框标题。 (字符串)(可选,默认为确认)
      • buttonLabels:指定按钮标签的字符串数组。 (数组)(可选,默认为 [OK,Cancel])

      您可以将 buttonLabels 更改为 ["Accept","Ignore"] 满足您的需求。

      示例

      function onConfirm(buttonIndex) {
          alert('You selected button ' + buttonIndex);
      }
      
      navigator.notification.confirm(
          'You are the winner!', // message
           onConfirm,            // callback to invoke with index of button pressed
          'Game Over',           // title
          ['Accept','Ignore']     // buttonLabels
      );
      

      【讨论】:

      • 我发现我被点击了“接受”按钮。但我想将参数从服务器传递给我的“接受”按钮回调
      • @EjazMafat 您不能从服务器传递到回调,我认为您应该使用 $.get 或 $.ajax 进行服务器调用以获取这些参数,以便您可以传递它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 2012-06-16
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多