EddyVerbruggen / nativescript-plugin-firebase

:fire: NativeScript plugin for Firebase
https://firebase.google.com
MIT License
1.01k stars 448 forks source link

No background notifications on IOS // Version 5.0.2 #565

Closed Lacos247 closed 6 years ago

Lacos247 commented 6 years ago

Hi,

so far everything works fine but I don't get background notifications on ios. If I start the app - the notifications are delivered and shown with the local notifications plugin. Remote Notifications are activated in the apropriate Xcode Project (and "UIBackgroundModes" in Info.plist , too).

In main.ts (before bootstrap) I have the following code. My expectation is that the local notification is shown even if the app is in the background. What I am missing? Any help would be appreciated!!!

firebase.init({ // Optionally pass in properties for database, authentication and cloud messaging, // see their respective docs and 'iOSEmulatorFlush' to flush token before init. iOSEmulatorFlush: true, onPushTokenReceivedCallback: function(token) { console.log("Firebase push token: " + token); }, onMessageReceivedCallback: function(message) { console.log("scheduling test notification.."); LocalNotifications.schedule([{ id: 1, title: 'New Local Test Title', body: 'New Local Test message', ticker: 'The ticker', badge: 5, groupedMessages:["The first", "Second", "Keep going", "one more..", "OK Stop"], //android only groupSummary:"Summary of the grouped messages above", //android only ongoing: true, // makes the notification ongoing (Android only) smallIcon: 'res://heart', sound: "customsound-ios.wav", // falls back to the default sound on Android at: new Date(new Date().getTime() + (1 * 1000)) // 10 seconds from now }]).then( function() { console.log("Notification scheduled"); }, function(error) { console.log("scheduling error: " + error); } ); } }).then( (instance) => { console.log("firebase.init done"); }, (error) => { console.log("firebase.init error: " + error); } );

EddyVerbruggen commented 6 years ago

I think I'll just remove push notification support from this plugin 😞

EddyVerbruggen commented 6 years ago

I'm kidding of course.

Lacos247 commented 6 years ago

Please don't do that :)
Maybe someone has a workaround for this issue.

Lacos247 commented 6 years ago

There seems to be an issue with combination of your local notifications plugin. See: https://github.com/EddyVerbruggen/nativescript-local-notifications/pull/22

uzarsalan commented 6 years ago

Did you tried setTimeout(function(){firebase.init(...)}, 3000); ?

Lacos247 commented 6 years ago

Thank you but I have tried that delay.

anuragd7 commented 6 years ago

Just upgraded to version 5.0.4 from 3.11.2. Can confirm that background notifications on IOS, which were working in the earlier version of the plugin stopped working on IOS. Everything works fine on Android.

gamalsmartlink commented 6 years ago

@EddyVerbruggen I am facing same issue in iOS. If app is open, it is receiving notification in console but if app is in background, nothing in the console. After that if I open it, it appears in the console. Plugin version: 5.0.4

Lacos247 commented 6 years ago

I am using the latest local notifications (https://github.com/EddyVerbruggen/nativescript-local-notifications) plugin in combination, too.

Our App receives a firebase notification with this plugin and evaluates the data payload to show language dependent notifications. The notifications are sheduled with the local notifications plugin.

Problems (iOS): While in foreground, the App receives the notification and can shedule a local notification. But this notification does not appear (no error messages).

While in background, the App does not receive any notification. The App receives them if I open it.

One example of the push message which I am sending to firebase:

{ "to": "/topics/news", "content_available": true, "sound":"default", "click_action":"FCM_PLUGIN_ACTIVITY", "icon":"fcm_push_icon" }, "data": { "type": "NEWS", "deNews": "DE News Test Message!", "frNews": "FR News Test Message!", "itNews": "IT News Test Message!", "enNews": "EN News Test Message!" } }

BR, Lacos

EddyVerbruggen commented 6 years ago

@gamalsmartlink @Lacos247 Do you see a notification when the app is in the background? Not seeing the console log is normal as your app isn't resumed when a notification is received. Unless you've configured it to wake the app when a remote notification is received and (I think) you'll also need to specify something in your push payload:

screen shot 2017-12-13 at 17 26 25

I've never tested that scenario with this plugin but feel free to try.

@Lacos247 Local Notifications don't show when the app is in the foreground ==> use an alert if you really have to. Or a Toast. Or some custom UI. Options a plenty.

Lacos247 commented 6 years ago

@EddyVerbruggen Thanks for the suggestion. The background modes were set in the Xcode project. The desired functionality is that the app is woken up for some amount of time to handle the message payload (and to create a local notification for that). But this does not happen.

thomasmoon commented 6 years ago

I am experiencing the same problem. Even when background notifications are enabled in Xcode I'm not receiving those, only when the application is in the foreground. Using the nativescrcipt-plugin-firebase demo app. So +1 and sorry @EddyVerbruggen this has been such a headache, but hopefully you won't remove the functionality from the plugin, it's great! ;)

erkanarslan commented 6 years ago

I also have this problem. I am trying to make background notifications work but no success so far. I can receive foreground messages but cannot create locale notifications from them. So, I cannot create any notifications in the notification tray.

erkanarslan commented 6 years ago

Downgrading to 3.11.2 solves the problem. I think I will use that version until this problem is fixed.

spstratis commented 6 years ago

@erkanarslan What version of Nativescript and the tns-iOS/Android bundles are you using?

EddyVerbruggen commented 6 years ago

I’ve fixed a problem with background pushes on iOS in the latest version.

erkanarslan commented 6 years ago

@spstratis My native script and iOS/android versions are all 3.4.0. Version 3.11.2 of plugin-firebase had problems with Android. After that I tried 5.0.0 and it works for both Android and iOS.

p-3 commented 6 years ago

There is a high risk of having to go to mental healthcare soon, I've tried to fix the iOS app with push notifications for a week now without success. Messages only work when the app is up but not in the foreground. xcode is set to handle push notifications and remote notifications (background mode)

Is there any demo app with working background push notifications so you can compare what's wrong with my project.

EddyVerbruggen commented 6 years ago

@p-3 Yes, the demo in the repo.

erkanarslan commented 6 years ago

@p-3 Did you upload GoogleService-Info.plist file to firebase console. This fixed my problem.

indianazhao commented 6 years ago

@erkanarslan Did you mean download GoogleService-Info.plist file from firebase console?

Lacos247 commented 6 years ago

I don't get it. Implemented an Objective C POC for firebase yesterday and it works flawlessly. Still no background notifications with this plugin. Is this working correctly for anyone under iOS?

I am becoming desperate right now.

EddyVerbruggen commented 6 years ago

If you share a repo I can reproduce the issue with then I might find some time to look at your setup.

p-3 commented 6 years ago

This also worked for a little while but now it's completely dead again, I do not understand whats wrong.

If you have the opportunity to look at my project, I would be extremely happy. You can find my repo at https://github.com/p-3/dialog-app/

EddyVerbruggen commented 6 years ago

Hi @p-3 after cloning your app locally and fixing a little thingy the background notifications started to work. PR sent.

Lacos247 commented 6 years ago

What was wrong?

thomasmoon commented 6 years ago

@EddyVerbruggen @p-3 I have also had problems with background notifications, if there is any way to open up what potential little "thingies" can cause problems it would be helpful! 😀

p-3 commented 6 years ago

thanks eddy, you are the king..

I can share my working project in another repo without my personal keys tonight, I post the link here as soon as I've fixed the repo.

p-3 commented 6 years ago

Get an working NS project at https://github.com/p-3/ns-pushnotifications using eddy's amazing plugin for push notifications.

Just add you're firebase settings to App_Resources and run.

ghost commented 6 years ago

Same here. What was wrong?

p-3 commented 6 years ago

@spacetasse have you tested push notifications with https://github.com/p-3/ns-pushnotifications ?

indianazhao commented 6 years ago

@p-3 I've tested your example project with my settings. The following git status shows what files I've updated.

Changes not staged for commit:
  (use "git add/rm <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   app/App_Resources/Android/google-services.json
        modified:   app/App_Resources/iOS/GoogleService-Info.plist
        modified:   app/App_Resources/iOS/build.xcconfig
        deleted:    app/App_Resources/iOS/dialogapp.entitlements
        modified:   package.json

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        app/App_Resources/iOS/MediQNative2.entitlements

But I still get only the push notification when the app is in foreground. I made a video record for this behavior. https://www.youtube.com/watch?v=CEXevRdDuC8

Can you please point out what little thing @EddyVerbruggen fixed or any other advice? Thank you.

p-3 commented 6 years ago

@indianazhao i had same exact the same issue before it did re-create all my apple certifications. are you sure that all certifications is valid and correctly installed?

Check out https://www.youtube.com/watch?v=LBw5tuTvKd4 how do fix the certifications, you can skip the swift parts in the video.

Eddy fixed:

in the app.component.ts

ghost commented 6 years ago

@p-3 Thanks! That did the trick!

indianazhao commented 6 years ago

@p-3 Thank you!

I found that the message payload format can also affect the notification behavior. If someone still face the issue, try to add key notification into the JSON payload. For example,

{
  "to": "/topics/hotnews",
  "notification": {
    "title":"Finally...",
    "body":"I got the push notification!"
  },
  "data": {
    "foo": "More info..."
  }
}

I cannot make background notification work until I add it into the payload.

But it make the function onMessageReceivedCallback where I try to generate a local notification by another great plugin nativescript-local-notifications from @EddyVerbruggen won't be triggered. This would be another journey...

indianazhao commented 6 years ago

After I add "click_action":"FCM_PLUGIN_ACTIVITY" into the payload and the function onMessageReceivedCallback now can be triggered after user click the notification message.

{
  "to": "/topics/hotnews",
  "notification": {
    "title":"Finally...",
    "body":"I got the push notification!",
    "click_action":"FCM_PLUGIN_ACTIVITY"
  },
  "data": {
    "foo": "More info..."
  }
}
TheOnlyMatt commented 6 years ago

Did you send your notifications with postman or with the firebase dashboard ? Because I followed all the instructions stated in this thread (except for regenerating the apple certifications, cause I guess they are correct), and I still got the notification when app is in foreground, but it's still silent when in background. So I tried to set the title and body properties as you said, but it has no effect ...

Dashboard screenshot

p-3 commented 6 years ago

I am sending the messages through firebase cloud functions, which works flawless for me. i am triggering this through database changes.

indianazhao commented 6 years ago

@TheOnlyMatt I sent the message through Postman.

postman 2018-02-08 12-53-23

postman 2018-02-08 12-48-46

manijak commented 6 years ago

Mental-health is the correct name for sure... Going crazy with this thing.

Few weeks ago I set up firebase for the first time. After re-configuring my certificates and Firebase settings, everything was working fine. All notifications were working (foreground, background & suspended) both on iOS and Android. Even on emulator.

Then I spent a week setting up DynamicLinks and AdMob. After getting both of those working, I published the app to TestFlight. Now the notifications in iOS do not work anymore (only foreground).

I've done every posible test localy, doublechecked every setting. Background notifications do not work.

What the heck is going on? [cry-emoji]

EddyVerbruggen commented 6 years ago

@manijak make it easy for me to help you and I will - share a simple repo reproducing the issue and I'll try to make time to send a PR.

manijak commented 6 years ago

After a quick talk with Eddy here, found out that my provisioning profiles were messed up after all. XCode generated some weird stuff that caused them to be invalidated over night.

Notifications on iOS working now. 👍

EddyVerbruggen commented 6 years ago

@elimin8r Share a project and perhaps I can find some time this weekend to take a look.

EddyVerbruggen commented 6 years ago

@elimin8r a couple of things:

dbay920 commented 6 years ago

Thanks for the above advice, finally got push notifications to work with app in background :) Now I am getting "undefined" in alert box when app is in foreground :/ Any advice would be appreciated, thanks! https://github.com/dbay920/phn-app

EddyVerbruggen commented 6 years ago

@dbay920 Good. You're probably using the Firebase console to send messages. Can you try CURL instead to see if that makes a difference? The command is here: https://github.com/EddyVerbruggen/nativescript-plugin-firebase/blob/master/docs/MESSAGING.md#testing

sushant-j commented 6 years ago

I'm using latest version of the plugin, v5.1.7, and still background notifications aren't working for me on iOS.

Steps I have followed:

  1. Push notifications are enabled from XCode in Capabilities tab. And Entitlements file is added to the project.
  2. Info.plist is updated with following snippet:
    <key>UIBackgroundModes</key>
    <array>
    <string>remote-notification</string>
    </array>
  3. GoogleServices-Info.plist is copied into App_resources/ios/
  4. Required plugin in main.js: `require("nativescript-plugin-firebase");
  5. Called firebase.init() in the onLoad() event of my main screen.
    firebase.init({
    onMessageReceivedCallback: function (message) {
    console.log(`Title: ${message.title}`);
    console.log(`Body: ${message.body}`);
    // if your server passed a custom property called 'foo', then do this:
    console.log(`Value of 'foo': ${message.data.foo}`);
    alert("Notification!");
    }
    }).then(function (instance) {
    console.log("firebase.init done");
    }, function (error) {
    console.log("firebase.init error: " + error);
    }); 

Also, APN Authentication key is configured on Firebase console.

I've followed almost all previous suggestions on this issue, but nothing has worked for me so far.

EddyVerbruggen commented 6 years ago

@sushant-j How about sharing the project with me and buying me a beer to take a look? 🍻

sushant-j commented 6 years ago

I tried reproducing the issue in this repo, but the app is crashing when firebase.init() is called.

EddyVerbruggen commented 6 years ago

@sushant-j Require the plugin here. Let me know when the crash is fixed and the issue is reproduced. Then I'll take a look.

Do everyone a favor and ping me in an issue on your repo and continue the discussion there.