zanglengyu
zanglengyu

Reputation: 41

Not receiving Firebase Cloud Messaging Notifications in ios

I use the Firebase Cloud Messaging Notification in my ios's app, but when I tested the real machine, I found that I could not receive any push messages.

I use the firebase unity package for ios. I checked all the steps, but I don't know where the problem is.

I use the p8 APNs file, developer Indentities in keychain whit Push Notifications.

In xcode, I open the options(push notifications and background modes), but it cannot receive the push message, background or foreground.

The log in xcode console like this:

[Firebase/Messaging][I-FCM002010] The subscription operation is suspended because you don't have a token. The operation will resume once you get an FCM token.

[GULReachability][I-REA902004] Network status has changed. Code:2, status:Connected


FCM: Initialize Firebase Messaging
FCM: Initialize Firebase Messaging

FCM: Retrieve registration token
FCM: Retrieve registration token

RequestPermissionAsync completed

[Firebase/InstanceID][I-IID003012] Provisioning profile has specifically provisioned devices, most likely a Dev profile.
[Firebase/InstanceID][I-IID003013] APNS Environment in profile: development

Upvotes: 4

Views: 10563

Answers (2)

Alejandro Cotilla
Alejandro Cotilla

Reputation: 2621

To subscribe to a topic successfully you have to request the instanceID first.

FirebaseApp.configure()

UNUserNotificationCenter.current().delegate = self

let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
UNUserNotificationCenter.current().requestAuthorization(options: authOptions, completionHandler: {_, _ in })

application.registerForRemoteNotifications()

InstanceID.instanceID().instanceID { (result, _) in
    if result != nil {
        // Receive notifications from the "all" topic
        Messaging.messaging().subscribe(toTopic: "all")
    }
}

Upvotes: 3

Jake G
Jake G

Reputation: 1195

The subscription operation is suspended because you don't have a token. The operation will resume once you get an FCM token.

You need to register your iOS client's FCM Token.

Firebase has pretty details documentation for setting this up: Setting Up a Firebase Cloud Messaging Client App on iOS

The meat of it comes from including this code in your AppDelegate:

if #available(iOS 10.0, *) {
  // For iOS 10 display notification (sent via APNS)
  UNUserNotificationCenter.current().delegate = self

  let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
  UNUserNotificationCenter.current().requestAuthorization(
    options: authOptions,
    completionHandler: {_, _ in })
} else {
  let settings: UIUserNotificationSettings =
  UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
  application.registerUserNotificationSettings(settings)
}

application.registerForRemoteNotifications()

And then you need to handle the token you are given. You should store this token in your Firebase database, and then you will need to include it when you send push notifications.

Firebase has an example for sending push notifications, that includes a database structure for storing these tokens, located here: Send Firebase Cloud Messaging notifications for new followers.

Upvotes: 3

Related Questions