Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cordova Android Push Notification With Action Button

I have used Push Plugin , when i am sending push with action button 1)accept 2)ignore.

when notification came , i was clicked on "accept" button . but i want parameters with the "accept" button callback. from that i will identify with notification's "accept" was called.

code reference

//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();
        }

in code "app.onPushAccept" function is callback of "accept" button..

Please help me as soon as possible. Thank You..

like image 676
3 revs, 2 users 93% Avatar asked Apr 25 '16 08:04

3 revs, 2 users 93%


2 Answers

Android Push Notification (Only)

Step 1 - First of all go to given below directory

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

Step 2 - Open GCMIntentService.java File from above directory

Step 3 - Determine function calling "createActions" and Add actual parameter "requestCode" like...

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

Step 4 - Determine function definition "createActions" and Add formal parameter "int requestCode" like...

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

Step 5 - In function definition "createActions" and inside for loops Change second parameter from "i" to "requestCode" like...

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

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

Step 6 - After completing above all steps remove android platform if already added platform Then add android platform.

Sorry and improve if any mistake founds in my solution.

like image 104
LOVE Avatar answered Nov 10 '22 03:11

LOVE


Okay, first imagine you are sending the following payload:

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

and you have setup the following action button handlers:

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

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

so now you have two options you can either put something in the push payload that uniquely identifies the push that was received which will be put in data.additionalData or modify the callbacks to call another event handler:

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
  }
}
like image 2
Simon MacDonald Avatar answered Nov 10 '22 02:11

Simon MacDonald