I broke iOS push notifications, what am I doing wrong?
See original GitHub issueHi, I’m having a hard time getting the push notifications to work on iOS. It worked for a while, but then stopped working after some seemingly unrelated changes. Currently, it doesn’t matter what payload I try to send, the iOS app doesn’t seem to respond to it in any way, whether it’s in foreground, background or completely killed. Please let me know if I’m missing something.
What I’ve done
GoogleService-Info.plist
Add GoogleService-Info.plist to the iOS project and set it’s build action is BundleResource
. (Copy to Output Directory
is set to Do not copy
, not sure if that matters).
Enabled remote notifications
Enabled remote notification background mode in Info.plist:
<key>UIBackgroundModes</key>
<array>
<string>remote-notification</string>
</array>
Add FirebaseAppDelegateProxyEnabled
Added FirebaseAppDelegateProxyEnabled in the app’s Info.plist file and set it to No:
<key>FirebaseAppDelegateProxyEnabled</key>
<false/>
Initialize the plugin
Initialized the plugin in the FinishedLaunching
in AppDelegate.cs:
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
//code removed for brevity...
LoadApplication(Startup.BuildApp(ConfigureIosServices));
FirebasePushNotificationManager.Initialize(options, true);
return base.FinishedLaunching(app, options);
}
Overrides in AppDelegate
Added the following overrides to AppDelegate.cs
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
FirebasePushNotificationManager.DidRegisterRemoteNotifications(deviceToken);
}
public override void FailedToRegisterForRemoteNotifications(UIApplication application, NSError error)
{
FirebasePushNotificationManager.RemoteNotificationRegistrationFailed(error);
}
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
FirebasePushNotificationManager.DidReceiveMessage(userInfo);
completionHandler(UIBackgroundFetchResult.NewData);
}
Subscribe to topics and events
Subscribed to topics and subscribed to events in the OnStartup
method in App.xaml.cs
protected override async void OnStart()
{
//code removed for brevity...
//subscribe to multiple topics
CrossFirebasePushNotification.Current.Subscribe("general");
CrossFirebasePushNotification.Current.Subscribe("articles");
CrossFirebasePushNotification.Current.Subscribe("discussions");
//subscribe to push notification events
CrossFirebasePushNotification.Current.OnNotificationError += async (s, e) =>
{
await MainThread.InvokeOnMainThreadAsync(() => _customPushHandler.HandleErrorAsync(e));
};
CrossFirebasePushNotification.Current.OnTokenRefresh += (s, e) =>
{
_customPushHandler.HandleTokenRefresh(e);
};
CrossFirebasePushNotification.Current.OnNotificationReceived += async (s, p) =>
{
await MainThread.InvokeOnMainThreadAsync(() => _customPushHandler.HandleAsync(p.Data, true));
};
CrossFirebasePushNotification.Current.OnNotificationOpened += async (s, p) =>
{
await MainThread.InvokeOnMainThreadAsync(() => _customPushHandler.HandleAsync(p.Data, false));
};
CrossFirebasePushNotification.Current.OnNotificationAction += async (s, p) =>
{
await MainThread.InvokeOnMainThreadAsync(() => _customPushHandler.HandleAsync(p.Data, false));
};
CrossFirebasePushNotification.Current.OnNotificationDeleted += (s, p) =>
{
_customPushHandler.HandleDeleted(p);
};
}
Notification payload
Here’s an example of a payload I’m sending:
URL: https://fcm.googleapis.com/fcm/send
HTTP Method: POST
Headers:
- Content-Type: application/json
- Authorization: key={my-fcm-server-key}
{
"to": "/topics/general",
"notification":{
"body" : "General notification: Some notification text",
"title": "MyAppName",
"sound" : "default"
},
"data":{
"appActionType": "show-general-message",
"appActionParameter": "Some notification text"
}
}
I’ve also tried adding a combination of these in the payload:
"content_available": true
"priority" : "high"
Project settings
Here’s a list of my project settings
Used nuget packages
- Plugin.FirebasePushNotification v3.4.1
- Xamarin.Forms v5.0.0.2337
iOS build settings
iOS linker skip assemblies
Assemblies that the linker should skip (specified via a LinkDescription linker config file).
linker.config
<?xml version="1.0" encoding="utf-8" ?>
<linker>
<!--Attemt to fix push notifications-->
<assembly fullname="Firebase.CloudMessaging">
<type fullname="*" />
</assembly>
<assembly fullname="Firebase.Core">
<type fullname="*" />
</assembly>
<assembly fullname="Firebase.Installations">
<type fullname="*" />
</assembly>
<assembly fullname="Firebase.InstanceID">
<type fullname="*" />
</assembly>
<assembly fullname="Plugin.FirebasePushNotification">
<type fullname="*" />
</assembly>
</linker>
Thanks for any advice!
Issue Analytics
- State:
- Created 2 years ago
- Comments:6
I’m facing the exact same issue you faced using the same settings and steps you did. However, even after recreate the APNs Key and certificate and updated them on Firebase, the notifications are still not working 😦
The iOS app registers correctly and subscribes to the topic, however, I’m still not getting the notifications on iOS.
Anybody faced this issue recently?
After I wrote all the code as in “Getting Started” my iOS project was crashing on first boot. It didn’t say anything as an error message either. I finally found the solution to the problem. After installing this plugin, I solved my error by adding the Xamarin.Firebase.InstanceID and Xamarin.Firebase.CloudMessaging packages that it specified as dependency to the iOS project.
Also I was using Xamarin.Firebase.iOS.Analytics package in my project. Firebase.Core.App.Configure() in AppDelegate when using this package; I fixed all the problems by commenting the code as well.
Also, when you run or archive in release mode, the FirebaseCloudMessaing nuget package does not compile due to a reference error. To work around this, use the Xamarin.Firebase.iOS.CloudMessaging package version 4.7.1 instead of 8.10.0. There is a problem with the latest version.
If your iOS platform terminates at application launch in your Xamarin project, you can solve the problem by doing these. I already created an issue about it.
https://github.com/CrossGeeks/FirebasePushNotificationPlugin/issues/414