ionic-team / capacitor

Build cross-platform Native Progressive Web Apps for iOS, Android, and the Web ⚡️
https://capacitorjs.com
MIT License
11.46k stars 977 forks source link

Push Notification does not work with Data Messages #1928

Closed naveedahmed1 closed 4 years ago

naveedahmed1 commented 4 years ago

Description of the problem: When using Capacitor Push Notification Plugin, the notification is received when the app is in foreground (opened and running) but when app is in background i.e. closed or minimized no notification is displayed in notification center. When I launch app, the notification is shown there.

Affected platform

OS of the development machine

Capacitor version: 1.2.0

node version: 10.16.0

Steps to reproduce:

When using Capacitor Push Notification Plugin, the notification is received when the app is in foreground (opened and running) but when app is in background i.e. closed or minimized no notification is displayed in notification center. When I launch app, the notification is shown there.

Below the is notification data that I am sending from server:

{
  "soundname": "pop_drip",
  "actions": "[{\"icon\":null,\"callback\":\"onMessageReply\",\"foreground\":true,\"title\":\"Reply Now\"},{\"icon\":null,\"callback\":\"onMessageRemindLater\",\"foreground\":false,\"title\":\"Remind Later\"},{\"icon\":null,\"callback\":\"onMessageIgnore\",\"foreground\":false,\"title\":\"Ignore\"}]",
  "id": "4e20d621c9d648f98d17e58035412d4c",
  "body": "This is a test message from Naveed",
  "icon": "icon_silhouette",
  "type": "message",
  "color": "#3f51b5",
  "image": "https://cdn.mydomain.com/content/images/static/2ffdfgc2cec88af449f88c66102380073d7.jpg",
  "notId": "4e20d621c9d648f98d17e58035412d4c",
  "style": "inbox",
  "title": "New message from Naveed"
}

This works perfectly fine with Firebase plugin for web that I am currently using for my webstie as well as it works without any issue with cordova push notification plugin https://github.com/phonegap/phonegap-plugin-push.

I am following the official guide to setup push notifications https://capacitor.ionicframework.com/docs/guides/push-notifications-firebase/ and have below code in my app:

    // On succcess, we should be able to receive notifications
    PushNotifications.addListener('registration', 
      (token: PushNotificationToken) => {
        //send token to the server
      }
    );

    // Some issue with our setup and push will not work
    PushNotifications.addListener('registrationError', 
      (error: any) => {
        alert('Error on registration: ' + JSON.stringify(error));
      }
    );

    // Show us the notification payload if the app is open on our device
    PushNotifications.addListener('pushNotificationReceived', 
      (notification: PushNotification) => {
        alert('Push received: ' + JSON.stringify(notification));
      }
    );

    // Method called when tapping on a notification
    PushNotifications.addListener('pushNotificationActionPerformed', 
      (notification: PushNotificationActionPerformed) => {
        alert('Push action performed: ' + JSON.stringify(notification));
      }
    );
digaus commented 4 years ago

This is by design. Sending only data payload will not display a notifiation. The Phonegap Push Plugin has a custom implementation where it will display data payloads if they have a title and body

I created a plugin where you can customize if you want to show the notification. Currently only can set title and body. You could create your own Plugin which handles this.

https://github.com/digaus/capacitor-background-fcm

naveedahmed1 commented 4 years ago

Thanks for sharing this @digaus, any idea about showing Actions buttons on push notifications when using Capacitor?

digaus commented 4 years ago

Thanks for sharing this @digaus, any idea about showing Actions buttons on push notifications when using Capacitor?

Yes you could modify notification builder here: https://github.com/digaus/capacitor-background-fcm/blob/5e075162114c6e8bfd22d82da0bd488eb2d827ad/android/capacitor-background-fcm/src/main/java/com/digaus/capbackgroundfcm/BackgroundFCMService.java#L93

There you could include addAction with its own intent. I could modify my plugin to be able to include action buttons but currently am too busy.

naveedahmed1 commented 4 years ago

It would be really great, if you could please also add support for Action Buttons.

naveedahmed1 commented 4 years ago

I proposed Action Buttons as a feature for Capacitor Push Plugin as well.

https://github.com/ionic-team/capacitor/issues/1938

What if you could add the functionality in the original Capacitor Plugin? I had a brief conversation with @mhartington here https://twitter.com/mhartington/status/1171070552519389184 and it seems that the Capacitor team would also support such contribution.

yacut commented 4 years ago

Any update on this?

jcesarmobile commented 4 years ago

as digaus said, this is by design, data notifications are not shown, you can use LocalNotifications plugin to show a notification once the data is received

fromage9747 commented 3 years ago

as digaus said, this is by design, data notifications are not shown, you can use LocalNotifications plugin to show a notification once the data is received

You can't, as when the app is in the background, none of the code triggers. When it's in the foreground it works but not in the background.

https://github.com/ionic-team/capacitor/issues/1938 should be re-opened as it is a legitimate feature request.

digaus commented 3 years ago

as digaus said, this is by design, data notifications are not shown, you can use LocalNotifications plugin to show a notification once the data is received

You can't, as when the app is in the background, none of the code triggers. When it's in the foreground it works but not in the background.

1938 should be re-opened as it is a legitimate feature request.

That's not true. The WebView/JavaScript code does not get triggered. However you can still show a local notification if you program it yourself. Just check out my old plugin on how to achieve this.

Always starting the WebView just to handle the data is not a good solution and should not be done.

fromage9747 commented 3 years ago

as digaus said, this is by design, data notifications are not shown, you can use LocalNotifications plugin to show a notification once the data is received

You can't, as when the app is in the background, none of the code triggers. When it's in the foreground it works but not in the background.

1938 should be re-opened as it is a legitimate feature request.

That's not true. The WebView/JavaScript code does not get triggered. However you can still show a local notification if you program it yourself. Just check out my old plugin on how to achieve this.

Always starting the WebView just to handle the data is not a good solution and should not be done.

I will give your repo a try today. It says that you need to get your hands on a Mac for ios. I can provide you with my Mac VM via Remote Control? Or if you don't wish to, I can assist you in getting your own Mac VM up and running on your end.

fromage9747 commented 3 years ago

@digaus When trying to follow your instructions for "on your MainActivity.java file add import com.digaus.capbackgroundfcm.BackgroundFCM;" I get the below:

image

Let me know if I am doing something stupid.

fromage9747 commented 3 years ago

@digaus I tried building and running the app without the MainActivity.java changes and received the below:

image

App just shows a white screen with nothing loading.

I see that the main files/folders for your repo were changed some time ago. Perhaps it no longer works in the latest release?

Anyhoo, waiting on your feedback.

I also noticed that you don't have instructions for add listener push notification received. Only action performed?

fromage9747 commented 3 years ago

@digaus Ignore my previous messages. I will put this aside for now and forget about LocalNotifications as a workaround. It would appear that https://github.com/ionic-team/capacitor-plugins/issues/200 has been moved to "On deck ⚾ in Capacitor Engineering ⚡️" 6 days ago

kiranrayala77 commented 2 years ago

https://github.com/ionic-team/capacitor-plugins/issues/200. is not w3orking.

{ "to":"fTrG6-1RQJSJCfIawLYHUy:APA91bHa9JbyDBDSSGqTEhMT6VIWXRFdh_xBtUTF-Rd9WUBHpOLsTi4xZsFUZMFW7S8ZQHLJooHB00XMxj8uUiFvKztNbia0ObTOfL6buI3zBOgYsR3PmNQRquKWUNzq4Cg3E93BPw9n", "notification" : { "body" : "Hiii Survey", "title": "Survey APP Notification" }, "data" : { "surveyId": "53cab4f8-da84-4925-aad0-0bf82401df34", "title":"Test mapping issue" } }

ionitron-bot[bot] commented 1 year ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.