Cordova Android 使用操作按钮推送通知

Cordova Android Push Notification With Action Button

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

收到通知时,我点击了 "accept" 按钮。但我想要带有 "accept" 按钮回调的参数。从那我将识别通知的 "accept" 被调用。

代码参考

//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" 中函数是 "accept" 按钮的回调..

请尽快帮助我。 谢谢..

使用navigator.notification.confirm method of the plugin cordova-plugin-dialogs

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

语法

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

您可以将 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
);

Android 推送通知(仅限)

第 1 步 - 首先转到下面给出的目录

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

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

第 3 步 - 确定函数调用 "createActions" 和 添加实参 "requestCode" like...

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

第 4 步 - 确定函数定义 "createActions" 和 添加形式参数 "int requestCode" like...

     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 平台。

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

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

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