phonegap / phonegap-plugin-push

Register and receive push notifications
MIT License
1.94k stars 1.91k forks source link

Not calling .on('notification') when app is totally closed #2575

Open ikishanoza opened 6 years ago

ikishanoza commented 6 years ago

Expected Behaviour

When app is totally closed and when user click on notification tray its open the app but not calling the .on('notification') . Plugin must have some method to handle this case because this is very important part of PushNotification(when app is totally closed).

I google this and found some questions regarding the same like #1913 and #1673 all the answers point out at https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#application-force-closed I actually dont understand this sentence "If you add force-start: 1 to the data payload the application will be restarted in background even if it was force closed." does that means If i pass force-start : 1 in my notification payload then when app is totally closed and click on notification it will call the .on('notification') method ?

I tried this by setting force-start : 1 in my notification payload and observe the adb logs ..they defiantly process something but when i click on notifictaion tray nothing happens I mean .on('notification') handler is not called.

Any Idea what am I doing wrong ? @macdonst please help :)

Actual Behaviour

Not calling .on('notification') when app is totally close and user click on notification.

Reproduce Scenario (including but not limited to)

Steps to Reproduce

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

Android 8.0 Android 6.0

(Android) What device vendor (e.g. Samsung, HTC, Sony...)

Honor 8 lite, Redme note 3

Cordova CLI version and cordova platform version

cordova-android 7.1.0 cordova verion 8.0.0

Plugin version

2.2.3

Sample Push Data Payload

Sample Code that illustrates the problem

Logs taken while reproducing problem

macdonst commented 6 years ago

@KishanIos007 can you run adb logcat | grep Push while reproducing the problem so we can see what is going on?

ikishanoza commented 6 years ago

Sure but i am on windows so grep Push is not working .. anything similar to this for windows ? @macdonst and what about this statement "If you add force-start: 1 to the data payload the application will be restarted in background even if it was force closed." ? UPDATE I tried to send force-start: 1 to my notification payload and observe that app is started forcefully just blinks happen in my screen and also I observe the adb logs in android studio its like app started but still no luck on .on('notification') method..its still not calling ..

macdonst commented 6 years ago

@KishanIos007 eh, just post the whole log up somewhere like gist. With regards to force-start I don't want to get into that as it just adds another variable. I believe the root cause is the plugin fires the notification event before your listener is setup but I don't want to say for sure until I see some logs.

ikishanoza commented 6 years ago

Sure Let me just put logs from android studio..

ikishanoza commented 6 years ago

Here is the gist file when I send push notification with force-start : 1 -> https://gist.github.com/KishanIos007/fe6fc0c97f381ad5c97acc9ff4524595

maxkunitsa commented 6 years ago

I have same issue on Android 8. I received event .on('notification') when app is running, but not event if app was closed and i clicked on notification.

danielfnz commented 6 years ago

I have some issue on android 7.1.1 and phonegap-plugin-push: "^2.2.3". I received event .on('notification') when app is running, but not event if app was closed and i clicked on notification.

tangamampilia commented 6 years ago

I deleted my previous post because I want to add more info.

Reproduce Scenario 1

Reproduce Scenario 2

Reproduce Scenario 3

Reproduce Scenario 4

Example payload

{
    "priority": "high",
    "to": "FIREBASE TOKEN",
    "data": {
        "payload": {
            "pushType": 7,
            "title": "Title",
            "description": "Message",
            "source": "https://www.google.com.mx/"
        }
    },
    "notification": {
        "title": "Title",
        "body": "Message",
        "sound": "default",
        "icon": "drawable-hdpi-icon"
    }
}

Plugin version 2.2.3

Thanks.

McSam94 commented 6 years ago

I'm having the same issue for my android phone...it's working fine when the app run on background. However, if i terminate the app from task manager it will still receive a notification at the notification panel but on.('notification') wont trigger.

jcesarmobile commented 6 years ago

Check the FCM docs, if you send notification and data payload and the app is in the background/closed on.('notification') won't trigger until you tap the notification in the tray and only with the data part. If yo want on.('notification') to trigger, then don't send notification key, just data. https://firebase.google.com/docs/cloud-messaging/android/receive

maxkunitsa commented 6 years ago

@jcesarmobile if you will send only data then notification will be not visible in system tray.

jcesarmobile commented 6 years ago

yeah, tell that to Google

tangamampilia commented 6 years ago

Actually as I can understand, you can send both notification and data payload. I'm quoting the FCM docs.

Handle notification messages in a backgrounded app

When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default.

This includes messages that contain both notification and data payload (and all messages sent from the Notifications console). In these cases, the notification is delivered to the device's system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

I didn't clarify it (I edited the post), but I'm actually taping the notification and the on.('notification') is not being triggered.

anothergituser commented 6 years ago

Exactly same issue here.. Your solution was correct. Also, i have a 'message' key in my data payload that takes the role of the "missing" notification key or no push notification will be shown.

Check the FCM docs, if you send notification and data payload and the app is in the background/closed on.('notification') won't trigger until you tap the notification in the tray and only with the data part. If yo want on.('notification') to trigger, then don't send notification key, just data. https://firebase.google.com/docs/cloud-messaging/android/receive

tangamampilia commented 6 years ago

Hi, I did a deep investigation. This works as expected with Android (foreground & background), but no notification show up on iOS.

{  
   "priority":"high",
   "registration_ids":[XXXXXXXXX ],
   "data":{  
      "message":"Message",
      "title":"Title",
      "vibrate":1,
      "sound":1,
      "payload":{  
         "pushType":7,
         "title":"Title",
         "description":"Description",
         "source":"http:\/\/google.com.mx\/"
      }
   }
}

This works as expected on iOS, but the on.('notification') don't trigger if the Android app is in the background or killed.

{  
   "priority":"high",
   "to":"XXXXXXXXXXX",
   "data":{  
      "payload":{  
         "pushType":7,
         "title":"Title",
         "description":"Description",
         "source":"https:\/\/github.com\/Maatwebsite\/Laravel-Excel"
      }
   },
   "notification":{  
      "title":"Title",
      "body":"Body",
      "sound":"default",
      "icon":"drawable-hdpi-icon"
   },
   "content_available":true
}

In my testings the main problem is the notification object. If you include it, then stop working on Android. If you remove it, then iOS is broken.

fredgalvao commented 6 years ago

@tangamampilia @maxkunitsa All of that is documented here for android and here for iOS+FCM+{android differences).

Please,

Tell us how we can make the docs better so that more people read it and understand that the payload for Android and iOS+FCM can't be the same.

ikishanoza commented 6 years ago

Yes @fredgalvao I read that doc to I suggest you have to put some real example our there with the live payload and with different different use cases so that we can compare to our payloads and get better Idea how its works.. not to much description just real world examples :) please, let me know if there's anything I can help you with

fredgalvao commented 6 years ago

@KishanIos007 Yes there is: you can propose those changes yourself through a pull request. Link it to this issue if you will, so people can migrate the discussion and help build a better doc on this topic so many still get wrong.

lizqjue commented 5 years ago

Hi, following the thread it is not clear if this is an issue or a matter of configuration.

In my case, it is not working. My app is closed, and if a notification arrives, it appears in the tray bar and when the user taps on it, the application is opened. However, though in the moment that the app is opened, we call init method and register register and notification listeners, only register callback is called.

My payload only contains data, not notification: { "to": xxxx, "data": { "title":"Localized title", "message": "localized message", "payload": { "data": { "other data": "" } } } }

Could you please clarify if there is an issue or it should work (and how)?

Thank you in advance and kind regards

bhavinshiroya1994 commented 5 years ago

I have some issue on android 6.3.0 and phonegap-plugin-push: "^2.1.3". I received event .on('notification') when app is running, but not event if app was closed and i clicked on notification.

please let me know fast because angry my client

danielfnz commented 5 years ago

Any fix?

ZipoNamberwan commented 5 years ago

everything works fine in android lower than Oreo, i can receive notifications foreground, background even when app is idle (i leave it idle for hours). But when i tested it in Oreo and Pie, i can not receive notifications when i leave the app idle for hours probably because service is kiled automatically, though i still can receive notifications when in foreground and background. The real problem is when i leave the app idle for hours notifications tray did not show its work. Anyone experience the same?

guilhermegjr commented 5 years ago

Hey Guys,

I got it to work in android sending the payload as the follow example:

var admin = require('firebase-admin');

var message = {
    token: 'the registration token',
    data: {
        title: 'Anything',
        body: "Anything else",
        notId: '123'
        priority: '1',
        vibrate: '300',
        visibility: '1',
    },
};

// Send a message to the device corresponding to the provided registration token.
admin
    .messaging()
    .send(message)
    .then(response => {
        // Response is a message ID string.
        console.log('Successfully sent message:', response);
    })
    .catch(error => {
        console.log('Error sending message:', error);
    });

Note that all the content, including title and body are in data atribute of the message.

You can get more information in the official doc Android Behaviour -> Notification vs Data Payloads

guilhermegjr commented 5 years ago

Hey Guys,

I got it to work in android sending the payload as the follow example:

var admin = require('firebase-admin');

var message = {
  token: 'the registration token',
  data: {
      title: 'Anything',
      body: "Anything else",
      notId: '123'
      priority: '1',
      vibrate: '300',
      visibility: '1',
  },
};

// Send a message to the device corresponding to the provided registration token.
admin
  .messaging()
  .send(message)
  .then(response => {
      // Response is a message ID string.
      console.log('Successfully sent message:', response);
  })
  .catch(error => {
      console.log('Error sending message:', error);
  });

Note that all the content, including title and body are in data atribute of the message.

You can get more information in the official doc Android Behaviour -> Notification vs Data Payloads

The message payload for iOS is as follow:

var message = {
    token: registrationToken,
    notification: {
        title: 'Anything',
        body: "Anything else",
    },
    data: {
        priority: '1',
        vibrate: '300',
        visibility: '1',
        notId: '123',
    },
};
jtibbles commented 5 years ago

Hi, I did a deep investigation. This works as expected with Android (foreground & background), but no notification show up on iOS.

{  
   "priority":"high",
   "registration_ids":[XXXXXXXXX ],
   "data":{  
      "message":"Message",
      "title":"Title",
      "vibrate":1,
      "sound":1,
      "payload":{  
         "pushType":7,
         "title":"Title",
         "description":"Description",
         "source":"http:\/\/google.com.mx\/"
      }
   }
}

Sadly this is also not working now. The Android example you have given will only work if the app is open and in the foreground. In the background the notification is never triggered. It seems something has changed again since you tested this.

jtibbles commented 5 years ago

Hey Guys, I got it to work in android sending the payload as the follow example:

var admin = require('firebase-admin');

var message = {
    token: 'the registration token',
    data: {
        title: 'Anything',
        body: "Anything else",
        notId: '123'
        priority: '1',
        vibrate: '300',
        visibility: '1',
    },
};

// Send a message to the device corresponding to the provided registration token.
admin
    .messaging()
    .send(message)
    .then(response => {
        // Response is a message ID string.
        console.log('Successfully sent message:', response);
    })
    .catch(error => {
        console.log('Error sending message:', error);
    });

Note that all the content, including title and body are in data atribute of the message.

Hi, I can only see this working on Android if my app is open in foreground. It isn't being called if the app is in the background (Android 8)

isaacbatst commented 5 years ago

It's happening the same here on Android 8 MIUI 10.3 (xiaomi). The event "notification" does not trigger when the app is closed, but it works when the app is in foreground or background

isaacbatst commented 5 years ago

using this structure:

    "to" => $token,
    "data" => array(
      "title" => utf8_encode($title),
      "body" => utf8_encode($body),
      "id_job" => $id_job,
     )
jtibbles commented 5 years ago

Has anybody found a fix? I can confirm on Android 8, 9 and 10 the push notification does not run within the app if the app is closed or in the background at the moment the user receives the message. Clicking on the the notification simply opens the app and does nothing.

The only solution I can think of is to make my own secondary checking system by storing all messages in a DB with a "read/unread" flag. A message becomes read only if the message is displayed within the app. On loading the app call the latest "unread" message from the DB and show it within the app, then flag it as read as we know the user has absolutely seen this message within the app. Seems like a much safer approach as Android continue to chage the way push messages work.

isaacbatst commented 5 years ago

I made it for all the situations (foreground, background and closed) by using a different strucure for each OS.

IOS

   $fields = array(
        "to" => $token,
        "notification" => array(
            "title" => utf8_encode($title),
            "body" => utf8_encode($body),
        ),
        "data" => array(
            "ids_job" => $id_job,
        ),
    );

Android: `

  $fields = array(
        "to" => $token,
        "data" => array(
            "id_job" => $id_job,
            "title" => utf8_encode($title),
            "body" => utf8_encode($body),
         ),
     );

`

jtibbles commented 5 years ago

I made it for all the situations (foreground, background and closed) by using a different strucure for each OS.

IOS

   $fields = array(
        "to" => $token,
        "notification" => array(
            "title" => utf8_encode($title),
            "body" => utf8_encode($body),
        ),
        "data" => array(
            "ids_job" => $id_job,
        ),
    );

Android: `

  $fields = array(
        "to" => $token,
        "data" => array(
            "id_job" => $id_job,
            "title" => utf8_encode($title),
            "body" => utf8_encode($body),
         ),
     );

`

Android 8+ won't display in the notification area if the notification parameter is removed. Data parameter only works if app is open and I'm foreground, and displays directly in the app.

isaacbatst commented 5 years ago

@jtibbles that's a new thing? I tested it in 31/07/19 with this plugin and it was working, but since then i have been using cordova-plugin-firebasex.

jtibbles commented 5 years ago

@isaacbatst what version of Android are you testing on? Apparently its new since 2019

isaacbatst commented 5 years ago

that's strange, it was tested on android 6 and 8.1

when i find a break, i'll create a basic project to have sure if it's still working and then post here

jtibbles commented 5 years ago

thanks @isaacbatst , I've been struggling with this for a while. Other people seem to be having the same problem: https://github.com/phonegap/phonegap-plugin-push/issues/2850

As a reference, here is my sample payload:

$notification = array ( 'body' => "some body text", 'title' => "some title text", "subtitle"=> "some subtitle", "icon" => 'notification_icon', "content_available" => "1",
); $data = array ( "title" =>"some other title, overridden by notification title" "message" => "some other message, which overrides notification body" "content_available" => "1", ); $fields = array ( 'registration_ids' => $registrationtokens, 'notification' => $notification, 'data' => $data, "priority" => "high", "content_available" => true );

If app is in background or closed the Notification area receives the message - clicking the notification opens the app but doesn't do anything else. If all is in foregroundthe Notification area doesn't receive the message (fine), but does receive the message in the app.

GitAleB commented 4 years ago

The behaviour is by design: Have a look at: https://github.com/phonegap/phonegap-plugin-push/blob/master/docs/PAYLOAD.md#notification-vs-data-payloads i.e.: the only way to get notifications in fore- and background mode is to structure you payload as described in that document. In other words: get rid of the "notification" property and put your data into the "data" property. This will cause the PushObject.on("notification"....) event being fired for all situations (fore-, background and cold start)

daveed80 commented 4 years ago

Quick question along these same lines....

When a notification is received, and the app is opened by directly clicking on the app from a closed state (not by clicking on the notification from the tray), shouldn't the on notification event fire and the notification be presented? If I click on the notification from the notification center, the app opens (from a closed state) and the event fires without issue.

This is for iOS 13.3.1/iPhone 11, and below is the payload...

{
    "aps": {
        "alert": "c",
        "sound": "default",
        "content-available": "1"
    },
    "notId": "2ffefbbe-28fd-42ab-b503-a1a68205b5be"
}