Cordova Android推送通知带有操作按钮

6
我使用了 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”函数是“接受”按钮的回调函数。

请尽快帮我,谢谢。

3个回答

3

安卓推送通知(仅限)

步骤1 - 首先进入以下目录:

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

步骤2 - 打开上述目录中的GCMIntentService.java文件

步骤3 - 确定调用“createActions”函数并添加实际参数“requestCode”,如下所示...

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

第四步 - 确定函数定义“createActions”并添加形式参数“int requestCode”,如下所示...
     private void createActions(Bundle extras, NotificationCompat.Builder mBuilder, Resources resources, String packageName, int notId,int requestCode)

第五步 - 在函数定义“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,有没有办法实现类似这样的东西?我不想干涉插件文件夹以实现此功能。 - Dhruv Singhal
这是因为在ionic构建apk时生成了该文件夹。换句话说,我能否通过更改config.xml和/或package.json中的某些内容来实现这一点? - Dhruv Singhal

2

好的,首先想象一下您正在发送以下有效载荷:

{
    "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
  }
}

0

使用cordova-plugin-dialogs插件的navigator.notification.confirm方法。

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

语法

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

您可以将buttonLabels更改为["接受","忽略"]以满足您的需求。

示例

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
);

我已经确认我点击了“接受”按钮。但我想从服务器传递参数到我的“接受”按钮回调函数。 - Ejaz Mafat
@EjazMafat,你不能从服务器传递到回调函数,我认为你应该使用$.get或$.ajax进行服务器调用以获取这些参数,以便您可以传递它。 - Arpit Vasani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接