iOS Firebase Push Notifications:How to Give Firebase User's Device Token and Send Notification

iOS Firebase Push Notifications : How To Give Firebase User's Device Token And Send Notification

Updated: As of Firebase 4.0.4, you can follow https://github.com/onmyway133/blog/issues/64

HOW APNS DEVICE TOKEN IS HANDLED

I've been reading Send a Notification to a User Segment on iOS but there is no mention of APNS device token, which is crucial to push notifications.

So Firebase must be doing some swizzling under the hood. In fact it is. Reading backend documentation Downstream Messages gives us the idea

Swizzling disabled: mapping your APNs token and registration token

If you have disabled method swizzling, you'll need to explicitly map your APNs token to the FCM registration token. Override the

methods didRegisterForRemoteNotificationsWithDeviceToken to retrieve
the APNs token, and then call setAPNSToken.

func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenTypeSandbox)
}

I particularly try to avoid swizzling as much as possible. Reading Migrate a GCM Client App for iOS to Firebase Cloud Messaging gives us how to do disable it

Enabling/disabling method swizzling

Method swizzling available with FCM simplifies your client code.
However, for developers who prefer not to use it, FCM allows you to
disable method swizzling by adding the
FIRMessagingAutoRegisterEnabledflag in the app’s Info.plist file and
setting its value to NO (boolean value).

FCM swizzling affects how you handle the default registration token, and how you handle downstream message callbacks. Where

applicable, this guide provides migration examples both with and
without method swizzling enabled.

SHOW ME THE CODE

Have this in your Podfile

pod 'Firebase'
pod 'FirebaseMessaging'

Here is the completed code

import Firebase
import FirebaseMessaging

override func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()

NSNotificationCenter.defaultCenter().addObserver(self,
selector: #selector(tokenRefreshNotification(_:)),
name: kFIRInstanceIDTokenRefreshNotification,
object: nil)
}

// NOTE: Need to use this when swizzling is disabled
public func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {

FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox)
}

func tokenRefreshNotification(notification: NSNotification) {
// NOTE: It can be nil here
let refreshedToken = FIRInstanceID.instanceID().token()
print("InstanceID token: \(refreshedToken)")

connectToFcm()
}

func connectToFcm() {
FIRMessaging.messaging().connectWithCompletion { (error) in
if (error != nil) {
print("Unable to connect with FCM. \(error)")
} else {
print("Connected to FCM.")
}
}
}

public func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print(userInfo)
}

When to use iOS device token when sending push notifications through FCM?

Only an FCM token is required to send a message to a device. There is no Apple-specific data required. Firebase handles the details of each underlying push notification system.

Can I Send notifications to specific users in firebase cloud messaging console

You can send notifications from the console to specific users by following the steps below:

  1. Get the FCM registration tokens for the users' devices. You can check how to get the tokens for the devices depending on the platform here.
  2. Go to the project's Notification Composer.
  3. Enter the notification details i.e notification title(optional),
    notification text, notification image(optional), notification name(optional)
    .
  4. Click Send test message.
  5. Enter the FCM registration tokens.
  6. Click Test.

Unable to receive IOS notifications from Firebase Console

Go to firebase->Project Settings->Cloud Messaging -> then scroll down to Apple app configuration and make sure you've provided APNs Production Certificate or not, if not then generate production certificate and upload it there. Alternatively i suggest you to upload APNs Authentication Key instead of certificates.

Preview Image



Related Topics



Leave a reply



Submit