somewhat @ google i/o event google renovated firebase , added lot of new features, , touched on remaining ones. have been trying implement ios push notifications via firebase app through basic level, created simple app nothing besides receive remote push notifications. inside of firebase, have uploaded certificate , within xcode provisioning profiles have been added both target , project, , in firebase have uploaded correct certificate. below code contained inside of appdelegate.swift
file because viewcontroller.swift
"empty," did not include it. although there no crashes or runtime errors, when load app, accept notifications. then, exit app , turn off device. in firebase, send notification correct app. after couple of minutes, in firebase says notification "completed". however, never received notification on device. so, in conclusion, need solution send firebase devicetoken
, use 'firebase notifications' send push notification message. code or in general appreciated , hope helps future viewers. thank you! code in appdelegate.swift
:
import uikit import firebase import firebasemessaging @uiapplicationmain class appdelegate: uiresponder, uiapplicationdelegate { var window: uiwindow? func application(application: uiapplication, didfinishlaunchingwithoptions launchoptions: [nsobject: anyobject]?) -> bool { firapp.configure() let notificationtypes : uiusernotificationtype = [uiusernotificationtype.alert, uiusernotificationtype.badge, uiusernotificationtype.sound] let notificationsettings = uiusernotificationsettings(fortypes: notificationtypes, categories: nil) application.registerforremotenotifications() application.registerusernotificationsettings(notificationsettings) return true } func application(application: uiapplication, didregisterforremotenotificationswithdevicetoken devicetoken: nsdata) { print("device token: \(devicetoken)") } func applicationwillresignactive(application: uiapplication) { } func applicationdidenterbackground(application: uiapplication) { } func applicationwillenterforeground(application: uiapplication) { } func applicationdidbecomeactive(application: uiapplication) { } func applicationwillterminate(application: uiapplication) { } func application(application: uiapplication, didreceiveremotenotification userinfo: [nsobject : anyobject], fetchcompletionhandler completionhandler: (uibackgroundfetchresult) -> void) { print("messageid : \(userinfo["gcm.messgae_id"]!)") // or gcm_etc... print(userinfo) } }
updated: of firebase 4.0.4, can follow https://github.com/onmyway133/blog/issues/64
how apns device token handled
i've been reading send notification user segment on ios there no mention of apns device token, crucial push notifications.
so firebase must doing swizzling under hood. in fact is. reading backend documentation downstream messages gives idea
swizzling disabled: mapping apns token , registration token
if have disabled method swizzling, you'll need explicitly map apns token fcm registration token. override
methods
didregisterforremotenotificationswithdevicetoken
retrieve apns token, , callsetapnstoken
.
func application(application: uiapplication, didregisterforremotenotificationswithdevicetoken devicetoken: nsdata) { firinstanceid.instanceid().setapnstoken(devicetoken, type: firinstanceidapnstokentypesandbox) }
i particularly try avoid swizzling as possible. reading migrate gcm client app ios firebase cloud messaging gives how disable it
enabling/disabling method swizzling
method swizzling available fcm simplifies client code. however, developers prefer not use it, fcm allows disable method swizzling adding firmessagingautoregisterenabledflag in app’s info.plist file , setting value no (boolean value).
fcm swizzling affects how handle default registration token, , how handle downstream message callbacks.
applicable, guide provides migration examples both , without method swizzling enabled.
show me code
have in podfile
pod 'firebase' pod 'firebasemessaging'
here 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 use when swizzling disabled public func application(application: uiapplication, didregisterforremotenotificationswithdevicetoken devicetoken: nsdata) { firinstanceid.instanceid().setapnstoken(devicetoken, type: firinstanceidapnstokentype.sandbox) } func tokenrefreshnotification(notification: nsnotification) { // note: can nil here let refreshedtoken = firinstanceid.instanceid().token() print("instanceid token: \(refreshedtoken)") connecttofcm() } func connecttofcm() { firmessaging.messaging().connectwithcompletion { (error) in if (error != nil) { print("unable connect fcm. \(error)") } else { print("connected fcm.") } } } public func application(application: uiapplication, didreceiveremotenotification userinfo: [nsobject : anyobject]) { print(userinfo) }