push-plugin icon indicating copy to clipboard operation
push-plugin copied to clipboard

Data not being received in intent either in launching activity or application

Open reytum opened this issue 6 years ago • 6 comments

When a notification from tray is clicked the app launches but no data is received in the intent. I have tried receiving the data in app.ts and my first view classes, for the events launchEvent and resumeEvent, but the data is received as empty whereas i am getting the data in onMessageReceived callback (stringified data).

here's the login.js file'c code

   application.on(application.launchEvent, function(args) {
        var act = args.android;
         if (act) {
           var intent = act.getIntent();
           var extras = intent.getExtras();
           console.log("*****Received intent login : " + JSON.stringify(intent));
           console.log("*****Received extras login : " + JSON.stringify(extras));
          
         if (extras) {
              console.log("*****Received intent login : " + JSON.stringify(intent));
              console.log("*****Received extras login : " + JSON.stringify(extras));
          }
      }
    });

everything in this is coming as empty.

This is my onMessageReceievd callback code:

 pushPlugin.onMessageReceived(function callback(stringifiedData, fcmNotification) {
      var notificationBody = fcmNotification && fcmNotification.getBody();
      console.log("*********Recieved message from plugin : " + stringifiedData);
      //console.log("Message received!\n" + notificationBody + "\n" + fcmNotification.getTitle());
      console.log("Notification received!\n" + JSON.stringify(fcmNotification));
    });

And this is what is prints:

*********Recieved message from plugin : {"APPLOZIC_21":"{\"id\":\"11c59dc62877c832883a1cdde60217a20\",\"type\":\"APPLOZIC_21\",\"message\":236838,\"notifyUser\":true,\"totalUnreadCount\":0,\"sendAlert\":false,\"messageMetaData\":{}}","foreground":true}
JS: Notification received!
JS: null

The other issue is that the notification is received only a few time, when the app is in background. It happens randomly. Sometime the notification is received, sometimes it is not.

reytum avatar Jan 04 '18 07:01 reytum

You should use the application's resume event instead of launch to handle the received notification. Take a look at the demo app's code for the push plugin on how exactly to extract the data from the intent:

https://github.com/NativeScript/push-plugin/blob/84aa27777b48fbb6f6ac8c4ad75007808835f71f/demo/app/app.ts#L13

lini avatar Jan 04 '18 07:01 lini

@reytum do you currently have any launchMode configuration in your manifest?

jogboms avatar Jan 04 '18 07:01 jogboms

@jogboms I dont have any launch mode configured in Manifest, if you are referring to NativeScriptActivity. @lini I have tried the similar configuration in app.ts file as mentioned in the original question. But still getting the data as empty.

reytum avatar Jan 04 '18 09:01 reytum

Please give us a sample of the notification payload you use. For example, I am testing with the following:

{
    "notification": {
        "title": "My title",
        "text": "My text",
        "badge": "1",
        "sound": "default"
    },
    "data": {
        "foo": "bar"
    },
    "priority": "High",
    "to": "FCM_DEVICE_TOKEN"
}

I am seeing both the notification details (My title/My text) in the notification tray and when I tap on the notification and open the app, I am seeing the data there as well:

01-05 13:49:50.497: V/JS(9210): If your notification has data (key: value) pairs, they will be listed here:
01-05 13:49:50.498: V/JS(9210): google.sent_time: 1515152971335
01-05 13:49:50.498: V/JS(9210): foo: bar
01-05 13:49:50.498: V/JS(9210): from: 424050927453
01-05 13:49:50.498: V/JS(9210): google.message_id: 0:1515152971339391%0ce8d8740ce8d874
01-05 13:49:50.498: V/JS(9210): collapse_key: org.nativescript.ppTest

I am using the demo app from this repo and an Android 7.1.1 (API 25) device to test.

lini avatar Jan 05 '18 11:01 lini

@lini @jogboms Well we are using GCM's Message Builder to construct the payload in the backend. This is what we are receiving on the device side:

{  
   "data" : {
    "message" :  "{  
      "id":"3002d7f24542aab636d966231fbb1b99b",
      "type":"APPLOZIC_01",
      "message":{  
         "key":"4-null-1515571377786",
         "userKey":"03bd75b9-c1c0-4ea7-9a0f-f26325f7c752",
         "to":"reytum2",
         "contactIds":"reytum2",
         "message":"sdvfva",
         "sent":false,
         "delivered":false,
         "read":false,
         "deliveredValue":0,
         "sendToDevice":false,
         "shared":false,
         "createdAtTime":1515571377827,
         "createdAt":"Jan 10, 2018 8:02:57 AM",
         "type":4,
         "source":5,
         "status":0,
         "pairedMessageKey":"5-null-1515571377786",
         "applicationKey":"agpzfmFwcGxvemljchgLEgtBcHBsaWNhdGlvbhiAgICAuqiOCgw",
         "contentType":0,
         "senderName":"reytum1",
         "metadata":{  

         },
         "alert":true
      },
      "notifyUser":true,
      "totalUnreadCount":4,
      "sendAlert":true,
      "messageMetaData":{  

      }
   }" //this is stringified
},
"notification" : {
    "title" : "sdkjd",
    "body" : "hey thjbsd"
},
"to" : "token"
}

The notification is being constructed in the tray, but the data is not being received in the intent. However, in onMessageReceived callback function I am getting the stringified data and the respective notification values.

This is what I am using to receive the intent in app.ts file

import "./bundle-config";
import * as application from 'tns-core-modules/application';
//application.start({ moduleName: "main-page" });
application.on(application.resumeEvent, function(args) {
    // console.log("*****Received act  : " + JSON.stringify(act));
    //     console.log("*****Received intent  : " + JSON.stringify(intent));
    //     console.log("*****Received extras : " + JSON.stringify(extras));
    if (args.android) {
        //var intent = act.getIntent();
        const act = args.android;
        const intent = act.getIntent();
        const extras = intent.getExtras();
        //console.log("*****Received act  : " + JSON.stringify(act));
        console.log("*****Received intent login : " + JSON.stringify(intent));
        console.log("*****Received extras login : " + JSON.stringify(extras));
        if (extras) {
            // for (var key in extras) {
            //     console.log(key + ' -> ' + extras[key]);
            // }
            console.log("*****Received intent login : " + JSON.stringify(intent));
            console.log("*****Received extras login : " + JSON.stringify(extras));
        }
    }
  });
application.start({ moduleName: "views/login/login" });

reytum avatar Jan 10 '18 08:01 reytum

@lini @jogboms Any update on this?

reytum avatar Feb 01 '18 06:02 reytum