2016-04-25 68 views
3

我已经使用Push Plugin,当我发送推动按钮1)接受2)忽略。科尔多瓦Android推动通知与行动按钮

当通知来了,我被点击了“接受”按钮。但我想用“接受”按钮回调参数。从那我会确定与通知的“接受”被称为。

码参考

//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” 功能是 “接受” 按钮的回调..

请尽快帮助我。 谢谢。

回答

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

很抱歉,如果在我的解决方案中发现任何错误,请加以改进。

+0

非常感谢你,这个解决方案真的很有用.. –

0

使用插件cordova-plugin-dialogs

它会显示一个可自定义的确认对话框的navigator.notification.confirm方法。

语法

navigator.notification.confirm(message, confirmCallback, [title], [buttonLabels]) 
  • 消息:对话消息。 (字符串)
  • confirmCallback:按下按钮索引(1,2或3)或当没有按钮按下(0)时解除对话框时调用的回调。 (功能)
  • 标题:对话框标题。 (字符串)(可选,默认为确认)
  • buttonLabels:指定按钮标签的字符串数组。 (阵列)(可选,默认为[OK,取消])

你可以改变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 
); 
+0

我确定我点击了“接受”按钮。但我想从服务器传递参数到我的回调“接受”按钮 –

+0

@EjazMafat你不能从服务器传递回调,我认为你应该使用$ .get或$ .ajax进行服务器调用以获得那些参数你可以通过它。 –

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