2016-04-25 67 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 
    } 
}