Nativescript-plugin-firebase: Background notifications not working on iOS 13

Created on 25 Sep 2019  ·  25Comments  ·  Source: EddyVerbruggen/nativescript-plugin-firebase

Hi!

The plugin is really great and it worked perfectly on iOS 12, I was able to send silent push notifications that were correctly caught by the onMessageReceivedCallback while my app was in background, and then displayed with a LocalNotification. However, after updating to iOS 13 and Xcode 11, background notifications are not processed anymore.

Inspecting in the iOS logs I can see the notifications arrive to the device, but then they are not processed by the app's callback. Is it just me or it's a problem you noticed too on iOS 13?

Thank you!

iOS

Most helpful comment

+1 plugin working with xcode 11
but not working with iOS 13

All 25 comments

+1 plugin working with xcode 11
but not working with iOS 13

Is this with external_push_client_only (see firebase.nativescript.json) set to true or false? So effectively: are you respectively using APNs directly or through Firebase Messaging?

I receive foreground and background notifications on my iOS 13 device without issues when using Firebase Messaging. I had to fix an issue for non-Firebase push through (the received token was incorrect), but I think that was not the issue you have.

To recreate your issue I have to know the exact payload you're using to send the silent notification. A CURL command like this would be most useful (this is what I just used to test):

curl -X POST --header "Authorization: key=MY_SERVER_PUSH_KEY" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"notification\":{\"title\": \"My title\", \"text\": \"My text\", \"badge\": \"1\", \"sound\": \"default\"}, \"data\":{\"foo\":\"bar\"}, \"priority\": \"High\", \"to\": \"cyjiy-p06DQ:APA91bESeQiQrI8KqNwgDaj7s8onBbmxia_HksAU0bWf5EBhiHjoAdjS6vCs-e78xKrEz0GaARiL_z9euGozZxoeBR0khiqOpwLRhBTNeMo051iF15vQgOolrFfmIWsAeEdAgh5BjDJL\"}"

```{
"external_push_client_only": false,
"using_ios": true,
"using_android": true,
"analytics": false,
"firestore": false,
"realtimedb": false,
"authentication": true,
"remote_config": false,
"performance_monitoring": false,
"messaging": true,
"in_app_messaging": true,
"crashlytics": false,
"storage": false,
"functions": false,
"facebook_auth": false,
"google_auth": false,
"admob": false,
"dynamic_links": false,
"ml_kit": false
}

app.js

const firebase = require("nativescript-plugin-firebase");

firebase.init({})
.then(() => console.log("[$_fcm] Firebase initialized"))
.catch(error => console.log("[$_fcm] Error initializing Firebase: ", error));
const app = new Vue({
store,
render: (h) => h(layout, [h(App, {slot: 'mainContent'})])
});

app.$start();

When user click login

async initPushFCM() {
if (this.successInitFCM)
return true;
this.messaging
.registerForPushNotifications({
autoClearBadge: true,
showNotifications: true,
showNotificationsWhenInForeground: true,
onPushTokenReceivedCallback: (token) => {
console.log("[$_fcm] Firebase plugin onPushTokenReceivedCallback: " + token);
},
onMessageReceivedCallback: (message) => {
console.log("[$_fcm] Firebase plugin onMessageReceivedCallback: ", message);
},
})
.then(() => {
console.log("[$_fcm] Firebase registerForPushNotifications");
return true;
})
.catch(error => {
console.log("[$_fcm] Error registerForPushNotifications Firebase: " + error);
return false;
});
this.successInitFCM = true;
},

Server send example 

const app = require("firebase-admin");

const serviceAccount = require("./../../config/boxexchanger-control-firebase-adminsdk-q9gyf-6f3f3b38dd.json");
app.initializeApp({
credential: app.credential.cert(serviceAccount),
databaseURL: "https://boxexchanger-control.firebaseio.com"
});

const fcm = app.messaging();

const message = {
token: '___TOKEN_FROM_DIVICE__',
notification: {
title: 'test1',
body: 'test1',
// sound: 'default',
},
apns: {
payload: {
aps: {
badge: 2
}
}
}
};

return fcm.send(message)
.then(console.log) // had success id
.catch(err => {
console.error('Error send FCM:', err, message);

return {err: err}

});
```

my account has APNs cert

iphone 5s ios 11 worked foreground and background

iphone 6s ios 13 i can have a callback when app in foreground
onMessageReceivedCallback
but dosen't work in background

@b-m-9 Can you share that app with me? Nothing obvious stands out from these snippets..

I've share git with app
git: https://gitlab.com/boxexchanger/exchanger-app
Branch: dev

How it work ?

Init
app/app.js
config
firebase.nativescript.json
plugin
app/modules/fcm.js
component with login who call init plugin
app/pages/auth/sign-in-use-form.vue
And server code i've send on email.

🙏 Thank you.

@b-m-9 Thx, is there a login/QR I can use to log in?

URL: empty
Email: [email protected]
Password: qweqwe

or click Sign in with account
and make a long-tap (2-3s) on Label _Welcome to boxexchanger_

@b-m-9 Thanks. This is what I get when the app is killed or running in the background on my iPhone 11 Pro:

IMG_4132

So your app seems to work perfectly fine.

Steps I took

  1. Cloned the repo.
  2. Swapped your GoogleService-Info.plist for the one in my /demo app folder (so I can send notifications from FCM).
  3. Changed the app id in package.json to match the one in the aforementioned plist file (org.nativescript.firebasedemo).
  4. Start the app and login with the suggested long-tap.
  5. After the app registered for push notifications I grabbed the logged device id and added it to this CURL command, resulting in the sccreenshot above:
curl -X POST --header "Authorization: key=MY_SERVER_KEY" --Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send -d "{\"notification\":{\"title\": \"My title\", \"text\": \"My text\", \"badge\": \"1\", \"sound\": \"default\"}, \"data\":{\"foo\":\"bar\"}, \"priority\": \"High\", \"to\": \"DEVICE_REGISTRATION_ID\"}"

So yeah, maybe you can use that CURL command as well (change the placeholder MY_SERVER_KEY and DEVICE_REGISTRATION_ID).

For anyone interacting with APNs directly (I guess Firebase already does this when using FCM), I just stumbled upon this: https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns?language=objc

TL;DR: it's now required to set a apns-push-type header, otherwise notifications may NOT be delivered to iOS 13 devices.

Thank you so mush I will test and after I will leave feedback
Have a good day.

maybe you carried out some additional actions, I took your request, tried to send it and received the same error on 5s comes on 6s does not come notification in the background

I run with cmd

tns run ios --bundle --hmr --provision profile.name.example

Nope, my git changes showed nothing else. I did ‘tns run ios’.

when i do tns build and upload in testflight it working but when i do tns run ios --bundle --hmr --provision profile.name.example push isn't working for me it's okay but meaby we have problem with plugin

If you run tns run ios locally (instead of bundle and hmr), does that work?

I have next step
Update nodejs 12
Remove plugins hook platforms
run ``tns run ios --provision net.boxexchanger.control.dev"```

but problem still

and when i've start app i have callbacks with messages with i've send in background

@EddyVerbruggen you are very good developer you are have the best support of your plugin thank you for your helping

Sorry can you say your versions
nodejs
tns
xcode

meaby my logs can help
i've chose intrasting logs

[Crashlytics:Crash] Warning: NSUncaughtExceptionHandler is 'unknown' in '/private/var/containers/Bundle/Application/387A30FD-7DC9-409D-ABF4-A549E6BAC6B7/exchangerapp.app/exchangerapp'


when i hide app

76.819s [C5] path:cancel
nw_flow_disconnected [C5.1 IPv4#10b1f61d:5228 cancelled socket-flow ((null))] Output protocol disconnected
nw_connection_report_state_with_handler_on_nw_queue [C5] reporting state cancelled
Can't end BackgroundTask: no background task exists with identifier 4 (0x4), or it may have already been ended. Break in UIApplicationEndBackgroundTaskError() to debug.


6.5.0 - [Firebase/Messaging][I-FCM001000] FIRMessaging Remote Notifications proxy enabled, will swizzle remote notification receiver handlers. If you'd prefer to manually integrate Firebase Messaging, add "FirebaseAppDelegateProxyEnabled" to your Info.plist, and set it to NO. Follow the instructions at:
https://firebase.google.com/docs/cloud-messaging/ios/client#method_swizzling_in_firebase_messaging
to ensure proper integration.

Oh sorry i don't know how but know is it working
my steps
run cmd

tns run ios --hmr --provision net.boxexchanger.control.dev

not work

// set new value true
Vue.config.silent = true

working

Vue.config.silent = false

and working (very strange problem)

issue can be closed

OK, sorry for the inconvenience.

Sorry, @b-m-9, is the onMessageReceivedCallback working for your app? Do you see the console log [$_fcm] Firebase plugin onMessageReceivedCallback... when you receive a push notification while your app is in background? I can receive background notifications with alert/banner, they are showing up correctly, my issue is only with silent notifications (content_available: true) that are not processed by the callback, it doesn't fire.

@xilefff yes onMessageReceivedCallback works but only when the application is active if it works in the background this callback will not work

@xilefff Meaby i can help you you can write me on Telegram or Skype

@xilefff
Did you manage to fix your issue? I'm having the same problem with content_available: 1 not being received by the onMessageReceivedCallback.

A same problem with me. I received message in console in xcode when app from foreground to background
Can't end BackgroundTask: no background task exists with identifier 15 (0xf), or it may have already been ended.
Has a discuss about this problem in forums developer of apple https://forums.developer.apple.com/thread/121990

Same here, sometimes the callback addOnMessageReceivedCallback is called when the app is in Background and sometimes not. And we have no hint, what the Problem is.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thunder413 picture thunder413  ·  3Comments

andrewexton373 picture andrewexton373  ·  4Comments

NNieto picture NNieto  ·  4Comments

b02505048 picture b02505048  ·  3Comments

bunower picture bunower  ·  3Comments