0

So I just implemented (well attempted to) Push Notifications for my app. I have sorted all the certificates out and have everything running within Xcode. I uploaded my .p12 file to Firebase in the development section and even downloaded the provisioning profile and reinstalled it into my project.

This is the code in my AppDelegate.swift file

Updated code:

import UIKit
import Firebase
import FirebaseMessaging

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var storyboard: UIStoryboard?

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

        // Override point for customization after application launch.

        self.storyboard =  UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
        let currentUser = FIRAuth.auth()?.currentUser
        if currentUser != nil
        {
            self.window?.rootViewController = self.storyboard?.instantiateViewControllerWithIdentifier("tBVC")
        }
        else
        {
            self.window?.rootViewController = self.storyboard?.instantiateViewControllerWithIdentifier("loginScreen")
        }

        return true
    }

    func registerForPushNotifications(application: UIApplication) {
        let notificationSettings = UIUserNotificationSettings(
            forTypes: [.Badge, .Sound, .Alert], categories: nil)
        application.registerUserNotificationSettings(notificationSettings)
    }

    func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
        if notificationSettings.types != .None {
            application.registerForRemoteNotifications()
        }
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }

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

        print(userInfo)
        print("MessageID:  \(userInfo["gcm_message_id"]!)")
        //
    }


}

When the app is running in the background no notifications display, even when my device is locked still nothing. The alert popped up asking if the app had permission to send notifications and I said yes.

I followed this tutorial

Any idea why my notifications aren't being displayed? In the Firebase Console it says that the status of the notification is 'Completed'

EDIT - Added image of my capabilities in Xcode enter image description here

Konsy
  • 306
  • 3
  • 22
  • Did you disable swizzling? – Arthur Thompson Jul 18 '16 at 21:15
  • sorry what exactly is that – Konsy Jul 18 '16 at 21:15
  • It is code that automatically associates your app's Instance ID token with your app's APNs token from Apple. It is on by default to this is unlikely to be the issue. – Arthur Thompson Jul 18 '16 at 21:17
  • @ArthurThompson where would i check for this? – Konsy Jul 18 '16 at 21:18
  • Could you implement didRegisterForRemoteNotifications and confirm that you are successfully generating an APNs token? Swizzling is turned off in your info.plist file. But it is on by default so it is unlikely that this is your issue. Confirming your APNs token would be much more informative. – Arthur Thompson Jul 18 '16 at 21:26
  • just checked my settings on my phone and the app doesn't have anything about Notifications – Konsy Jul 18 '16 at 21:28
  • I would have a look at the sample application that implements FCM on iOS and go from there, you will likely need all the implemented methods in the AppDelegate.swift file: https://github.com/firebase/quickstart-ios/blob/master/messaging/FCMSwift/AppDelegate.swift – Arthur Thompson Jul 18 '16 at 21:44
  • Did you get a token printed in your console? – Arthur Thompson Jul 18 '16 at 22:00
  • nope nothing at all, when i ran the app it gave me a breakpoint error under the gcm.message – Konsy Jul 18 '16 at 22:01
  • Apple is having problems with its sandbox APN today, that is probably why. More particulary, didRegisterForRemoteNotificationsWithDeviceToken does not get called anymore, so FireBase does not get a token and you will not get a push notification. I am facing the same problem today with firebase, it worked yesterday. https://forums.developer.apple.com/thread/52150 – Collinizer Jul 19 '16 at 11:13
  • @Collinizer ☺️ oh alright! Thank you for that information! I tried using Firebase with push notifications on a project a while back but it didn't work then, I'm going to just use OneSignal instead! Thanks though – Konsy Jul 19 '16 at 11:18
  • If Apple is having issues with sandbox APN then any service that sends to iOS devices will have issues, FCM, OneSignal, SNS etc. – Arthur Thompson Jul 19 '16 at 17:17

2 Answers2

1

I worked out the reason for why it was doing so! As @Collinizer stated there were issues with Apple and there APNS but it is all working now! I added push notifications in using OneSignal and they are working like a dream!

Thank you to all that helped :)

Konsy
  • 306
  • 3
  • 22
0

You need to configure set up of APNS device token which is crucial to push notifications with Firebase Cloud Messaging (FCM).

First let's back up a little bit and start by just seeing if we can at least get token success.

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

FIRApp.configure()
return true
}

func registerForPushNotifications(application: UIApplication) {
    let notificationSettings = UIUserNotificationSettings(
        forTypes: [.Badge, .Sound, .Alert], categories: nil)
    application.registerUserNotificationSettings(notificationSettings)
}

func application(application: UIApplication, didRegisterUserNotificationSettings notificationSettings: UIUserNotificationSettings) {
    if notificationSettings.types != .None {
        application.registerForRemoteNotifications()
    }
}


func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
let tokenChars = UnsafePointer<CChar>(deviceToken.bytes)
var tokenString = ""

for i in 0..<deviceToken.length {
    tokenString += String(format: "%02.2hhx", arguments: [tokenChars[i]])
}


FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown)
print("Device Token:", tokenString)
}
Edison
  • 11,881
  • 5
  • 42
  • 50
  • and... make sure Bundle ID is the same that one you set in GoogleService-Info. and make sure these are correct and match : http://i.stack.imgur.com/p5N2F.png. Goal is to test tokens first. – Edison Jul 18 '16 at 22:31
  • yeah it is all set up correctly in regards to the certificates and my firebase dashboard settings are right. My whole project just went back to a really old (the first version) of it ;-; meaning I just lost all of the new work even though it was saved... – Konsy Jul 18 '16 at 22:35
  • Oh and make sure when you're testing this AppDelegate.swift that you've commented out anything in ViewController.swift that isn't necessary which is anything that is not boiler-plate.. – Edison Jul 18 '16 at 22:43
  • thanks for the help but I'm just going to use One Signal instead for push notifications :l – Konsy Jul 18 '16 at 22:44