Lucè Brùlè Lucè Brùlè - 1 year ago 133
iOS Question

Firebase & Push Notifications / Cloud Messaging

I'm not able to get a single push notification sent to my device I've set up a demo app with their demo app-delegate, did sign it with a provisioning profile, with an push-notification enabled app and uploaded my .p12 to the firebase admin panel, but no matter what I try (resigning everything,.. and so on) I'm not able to send a push message through the firebase console.
What am I missing here?

Edit: I did try to send to the whole segment and also to a single device (with the id, given in the console)

import UIKit
import Firebase
import FirebaseInstanceID
import FirebaseMessaging

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Register for remote notifications
if #available(iOS 8.0, *) {
// [START register_for_notifications]
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
// [END register_for_notifications]
} else {
// Fallback
let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound]


// Add observer for InstanceID token refresh callback.
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification),
name: kFIRInstanceIDTokenRefreshNotification, object: nil)

return true

// [START receive_message]
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification

// Print message ID.
print("Message ID: \(userInfo["gcm.message_id"]!)")

// Print full message.
print("%@", userInfo)
// [END receive_message]

// [START refresh_token]
func tokenRefreshNotification(notification: NSNotification) {
let refreshedToken = FIRInstanceID.instanceID().token()!
print("InstanceID token: \(refreshedToken)")

// Connect to FCM since connection may have failed when attempted before having a token.
// [END refresh_token]

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

func applicationDidBecomeActive(application: UIApplication) {

// [START disconnect_from_fcm]
func applicationDidEnterBackground(application: UIApplication) {
print("Disconnected from FCM.")
// [END disconnect_from_fcm]

Answer Source

I don't see that you are calling setAPNSToken, perhaps that's what you are missing?

// AppDelegate.m
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    [firebaseManager didRegisterRemoteNotifications:deviceToken];

// FirebaseManager.swift
func didRegisterRemoteNotifications(deviceToken: NSData) {

    let type: FIRInstanceIDAPNSTokenType
    #if DEBUG
        type = .Sandbox
        type = .Prod

    FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: type)