Xcode 8.1 Push Notifications in Swift 2.3 with Firebase Integration Not Getting

ios10, Swift 3 and Firebase Push Notifications (FCM)

Inside method didRegisterForRemoteNotificationsWithDeviceToken, add the following code:

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("tokenString: \(tokenString)")
}

And do not forget to enable Push Notifications inside Capabilities.

Push Notifications not being received on iOS 10, but working on iOS 9 and before

Ok I have figured it out. I now have my original Push Notifications that was working on iOS 9 working on iOS 10 with Xcode 8 and Swift 2.3.

I implemented this by making the following changes to my AppDelegate:

1) On my project settings, under the Capabilities Tab, I scroll down to "Push Notifications" and turn it to "ON". This automatically generates an entitlements file that contains the key "APS Environment" and with value "development".

2) In AppDelegate.swift I make several code changes. I start by importing the UserNotifications framework:

import UserNotifications

Then I have AppDelegate implement the UNUserNotificationCenterDelegate protocol:

class AppDelegate: /*Some other protocols I am extending...*/, UNUserNotificationCenterDelegate {

Then I add the following method:

func registerForPushNotifications(application: UIApplication) {

if #available(iOS 10.0, *){
UNUserNotificationCenter.currentNotificationCenter().delegate = self
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert], completionHandler: {(granted, error) in
if (granted)
{
UIApplication.sharedApplication().registerForRemoteNotifications()
}
else{
//Do stuff if unsuccessful...
}
})
}

else{ //If user is not on iOS 10 use the old methods we've been using
let notificationSettings = UIUserNotificationSettings(
forTypes: [.Badge, .Sound, .Alert], categories: nil)
application.registerUserNotificationSettings(notificationSettings)

}

}

Then I call this newly created function inside didFinishLaunchingWithOptions:

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

I leave my didRegisterForRemoteNotificationsWithDeviceToken method unchanged:

func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
//Implement this. Do something with the token I receive. Send it to my push notification server to register the device or something else I'd like to do with the token.
}

Now I implement two new methods for iOS 10 to handle the receiving of Push Notifications:

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {
//Handle the notification
}

@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {
//Handle the notification
}

I did not remove any of the methods I have previously implemented for Push Notifications in iOS 9.

APNS Firebase Notification failed to fetch token

1.Set Notification Observer in didFinishLaunchingWithOptions Method

2.And Set tokenRefreshNotification method then u get Token in this method.

See below Code

import Firebase
import FirebaseMessaging

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

NotificationCenter.default.addObserver(self,
selector: #selector(self.tokenRefreshNotification(notification:)),
name: NSNotification.Name.firInstanceIDTokenRefresh,
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)
}

Get push notification while App in foreground iOS

If the application is running in the foreground, iOS won't show a notification banner/alert. That's by design. But we can achieve it by using UILocalNotification as follows

  • Check whether application is in active state on receiving a remote

    notification. If in active state fire a UILocalNotification.

    if (application.applicationState == UIApplicationStateActive ) {

    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.userInfo = userInfo;
    localNotification.soundName = UILocalNotificationDefaultSoundName;
    localNotification.alertBody = message;
    localNotification.fireDate = [NSDate date];
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
    }

SWIFT:

if application.applicationState == .active {
var localNotification = UILocalNotification()
localNotification.userInfo = userInfo
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.alertBody = message
localNotification.fireDate = Date()
UIApplication.shared.scheduleLocalNotification(localNotification)
}

Cloud messaging handing terminate app

Here is the solution,

First Upload the necessary certificates in Firebase Console Then in your app enable Push Notifications and Background Modes -> Remote Notifications

After that in App Delegate use the code below(I specify the tricky line) :

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
registerForPushNotifications(application)
// Override point for customization after application launch.
// Use Firebase library to configure APIs
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]])
}

//Tricky line
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Unknown)
print("Device Token:", tokenString)
}

UNUserNotificationCenter did receive response with completion handler is never called iOS10, swift 2.3

Request identifier is not the notification category.

Just add this line:

content.categoryIdentifier = identifier

Update:
Just made a simple app. Everything seems to working fine:

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

UNUserNotificationCenter.current().delegate = self

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .badge, .sound]) { (granted, error) in
if granted {
self.registerCategory()
self.scheduleNotification(event: "test", interval: 3)
self.scheduleNotification(event: "test2", interval: 5)
}
}

return true
}

func registerCategory() -> Void{

let callNow = UNNotificationAction(identifier: "call", title: "Call now", options: [])
let clear = UNNotificationAction(identifier: "clear", title: "Clear", options: [])
let category : UNNotificationCategory = UNNotificationCategory.init(identifier: "CALLINNOTIFICATION", actions: [callNow, clear], intentIdentifiers: [], options: [])

let center = UNUserNotificationCenter.current()
center.setNotificationCategories([category])

}

func scheduleNotification (event : String, interval: TimeInterval) {
let content = UNMutableNotificationContent()

content.title = event
content.body = "body"
content.categoryIdentifier = "CALLINNOTIFICATION"
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: interval, repeats: false)
let identifier = "id_"+event
let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)

let center = UNUserNotificationCenter.current()
center.add(request, withCompletionHandler: { (error) in
})

}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
print("didReceive")
completionHandler()
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("willPresent")
completionHandler([.badge, .alert, .sound])
}

}

Update 2: Rewritten in Swift 2.3

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

var window: UIWindow?

func applicationDidFinishLaunching(application: UIApplication) {
UNUserNotificationCenter.currentNotificationCenter().delegate = self
UNUserNotificationCenter.currentNotificationCenter().requestAuthorizationWithOptions([.Badge, .Sound, .Alert]) { (granted, error) in
if granted {
self.registerCategory()
self.scheduleNotification("test", interval: 3)
self.scheduleNotification("test2", interval: 5)
}
}
}

func registerCategory() -> Void{

let callNow = UNNotificationAction(identifier: "call", title: "Call now", options: [])
let clear = UNNotificationAction(identifier: "clear", title: "Clear", options: [])
let category : UNNotificationCategory = UNNotificationCategory.init(identifier: "CALLINNOTIFICATION", actions: [callNow, clear], intentIdentifiers: [], options: [])

let center = UNUserNotificationCenter.currentNotificationCenter()
center.setNotificationCategories([category])

}

func scheduleNotification(event : String, interval: NSTimeInterval) {
let content = UNMutableNotificationContent()

content.title = event
content.body = "body"
content.categoryIdentifier = "CALLINNOTIFICATION"
let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: interval, repeats: false)
let identifier = "id_"+event
let request = UNNotificationRequest.init(identifier: identifier, content: content, trigger: trigger)

let center = UNUserNotificationCenter.currentNotificationCenter()
center.addNotificationRequest(request) { (error) in

}
}

func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void) {

print("willPresent")
completionHandler([.Badge, .Alert, .Sound])
}

func userNotificationCenter(center: UNUserNotificationCenter, didReceiveNotificationResponse response: UNNotificationResponse, withCompletionHandler completionHandler: () -> Void) {

print("didReceive")
completionHandler()
}

}


Related Topics



Leave a reply



Submit