Fel*_*rra 20 apple-push-notifications ios firebase swift
我有一个应用程序将用户的会话存储在NSUserDefaults中.当应用程序被强制关闭时,在初始验证数据控制器是否存在用户会话,以防如果将其发送到启动窗口,如下所示:
override func viewWillAppear(animated: Bool) {
self.view.hidden = true
let defaults = NSUserDefaults.standardUserDefaults()
if defaults.stringForKey("user") != nil
{
dispatch_async(dispatch_get_main_queue(), { () -> Void in
let viewController:UIViewController = self.storyboard?.instantiateViewControllerWithIdentifier("vistaInicio") as! ViewControllerInicio
self.presentViewController(viewController, animated: true, completion: nil)
})
}else
{
self.view.hidden = false
}
}
Run Code Online (Sandbox Code Playgroud)
直到今天,当我决定 在iOS上设置Firebase云消息客户端应用程序后,根据本教程更新firebase实施推送通知,这一直顺利进行.当他杀死应用程序并再次输入时出现问题,提供以下错误代码:
2016-05-19 16:05:27.647: <FIRInstanceID/WARNING> Failed to fetch APNS token Error Domain=com.firebase.iid Code=1001 "(full)"
2016-05-19 16:05:27.659: <FIRMessaging/INFO> FIRMessaging library version 1.1.0
2016-05-19 16:05:27.831: <FIRMessaging/WARNING> FIRMessaging registration is not ready with auth credentials
Unable to connect with FCM. Optional(Error Domain=com.google.fcm Code=501 "(null)")
Run Code Online (Sandbox Code Playgroud)
小智 35
这是解决方案,
首先在Firebase控制台中上传必要的证书然后在您的应用中启用推送通知和后台模式 - >远程通知
之后在App Delegate中使用下面的代码(我指定了棘手的行):
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)
}
Run Code Online (Sandbox Code Playgroud)
不要忘记AppDelegate:
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
registerForPushNotifications(application)
FIRApp.configure()
// Add observer for InstanceID token refresh callback.
NSNotificationCenter
.defaultCenter()
.addObserver(self, selector: #selector(AppDelegate.tokenRefreshNotificaiton),
name: kFIRInstanceIDTokenRefreshNotification, object: nil)
// Override point for customization after application launch.
return true
}
func registerForPushNotifications(application: UIApplication) {
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
print("===== didReceiveRemoteNotification ===== %@", userInfo)
}
func tokenRefreshNotificaiton(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.
connectToFcm()
}
func connectToFcm() {
FIRMessaging.messaging().connectWithCompletion { (error) in
if (error != nil) {
print("Unable to connect with FCM. \(error)")
} else {
print("Connected to FCM.")
}
}
}
Run Code Online (Sandbox Code Playgroud)
请确保在Info.plist中也这样做:FirebaseAppDelegateProxyEnabled = NO
我不知道,但现在我得到了print(...)中didReceiveRemoteNotification,但没有得到弹出.我从Firebase发送消息- >控制台 - >通知 - >单个设备并在此处复制我从Xcode控制台获取的令牌 - >func tokenRefreshNotificaiton
| 归档时间: |
|
| 查看次数: |
22968 次 |
| 最近记录: |