question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I broke iOS push notifications, what am I doing wrong?

See original GitHub issue

Hi, 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

image

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:closed
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

4reactions
vsimpleacommented, Oct 18, 2022

Hey @concer22 thanks for the advice! I actually did have both the APNs auth key and certificates uploaded to Firebase, so I didn’t think that could be the problem. However, I’ve created a new auth key and a new certificate to be sure. Once I’ve replaced the previous APNs key and certificate in Firebase, the notifications started working again. I’m not sure what’s been the problem, but it seems to be working now, so thank you!

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?

1reaction
Hamittrpncommented, May 11, 2022

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Notifications on my iPhone are not working
Notifications on my iPhone are not working. No longer receive notifications on apps that I set up to receive them except iMessage and...
Read more >
iPhone Notifications Not Working? Here's The Real Fix!
An Apple expert explains why iPhone notifications are not working and shows you how to fix the problem with a simple, step-by-step guide....
Read more >
Fix: iOS 16 / iOS 16.5 Notification Not Working or Showing ...
Solution 1: Check App Notifications Settings · Solution 2: Disable Focus or Do Not Disturb Mode · Solution 3: Turn Off Airplane Mode...
Read more >
Broken notifications - cannot find a fix! : r/ios
I've checked my app background refresh, which had turned itself off but even though that's back on, nothing. Restarted my phone. Updated.
Read more >
iOS Push Notification What am I doing wrong? [closed]
I have tried to send push notifications to my apps multiple times, but with no avail. I have followed many tutorials and followed...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found