ios 云消息处理终止应用程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37333177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cloud messaging handing terminate app
提问by Felipe Parra
I have an application that stores the user's session in NSUserDefaults. When the application is forced to close, in the initial verify whether the data controller user session there, in case if there sent it to the start window as follows:
我有一个应用程序将用户的会话存储在 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
}
}
This worked smoothly me until today when I decided to implement push notifications with updating firebase following this tutorial Setting up a Firebase Cloud Messaging Client App on iOS. The problem occurs when he killed the application and enter again gives the following error code:
这一直很顺利,直到今天我决定按照本教程在 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)")
回答by LoGoCSE
Here is the solution,
这是解决方案,
First Upload the necessary certificates in Firebase Console Then in your app enable Push Notifications and Background Modes -> Remote Notifications
首先在 Firebase 控制台中上传必要的证书然后在您的应用程序中启用推送通知和后台模式 -> 远程通知
After that in App Delegate use the code below(I specify the tricky line) :
之后在 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)
}
回答by Svitlana
Do not forgot in AppDelegate:
不要忘记在 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.")
}
}
}
Please make sure also to do in Info.plist: FirebaseAppDelegateProxyEnabled = NO
请确保也在Info.plist 中做:FirebaseAppDelegateProxyEnabled = NO
I don't know for now but I got the print(...)
in didReceiveRemoteNotification
but don't get the popup. I send the message from Firebase -> Console -> Notification -> Single deviceand copy here the token which I got from Xcode Console -> func tokenRefreshNotificaiton
我不知道,但现在我得到了print(...)
中didReceiveRemoteNotification
,但没有得到弹出。我从Firebase -> Console -> Notification -> Single device发送消息并将我从 Xcode Console 获得的令牌复制到这里 ->func tokenRefreshNotificaiton
回答by Mr.G
Answer is correct those are the steps , But also please check your device time . Since if your time and date is way too off it wont work
答案是正确的那些是步骤,但也请检查您的设备时间。因为如果你的时间和日期太远,它就行不通
回答by raver99
After having tripple checked the whole integration, for me the issue was that the test device had the datechanged to a week in advance. Probably the FCM SDK does some date based checks.
在tripple检查了整个集成之后,对我来说问题是测试设备的日期提前一周更改了。FCM SDK 可能会执行一些基于日期的检查。
The error could be less generic on the Firebase side, since I lost almost a day searching for a solution. Hope this helps.
Firebase 方面的错误可能不太普遍,因为我花了将近一天的时间来寻找解决方案。希望这可以帮助。