Ios 云消息传递应用程序
我有一个应用程序,它将用户的会话存储在NSUserDefaults中。当应用程序被迫关闭时,在初始阶段,验证data controller用户会话是否存在,如果存在,则将其发送到启动窗口,如下所示:Ios 云消息传递应用程序,ios,swift,firebase,apple-push-notifications,Ios,Swift,Firebase,Apple Push Notifications,我有一个应用程序,它将用户的会话存储在NSUserDefaults中。当应用程序被迫关闭时,在初始阶段,验证data controller用户会话是否存在,如果存在,则将其发送到启动窗口,如下所示: override func viewWillAppear(animated: Bool) { self.view.hidden = true let defaults = NSUserDefaults.standardUserDefaults() if default
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
}
}
直到今天,我决定在本教程之后通过更新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)")
2016-05-19 16:05:27.647:无法获取APNS令牌错误域=com.firebase.iid code=1001”(完整)
2016-05-19 16:05:27.659:FIRMessaging库版本1.1.0
2016-05-19 16:05:27.831:未使用身份验证凭据准备好FIRMessaging注册
无法与FCM连接。可选(错误域=com.google.fcm代码=501“(空)”)
这是解决方案
首先在Firebase控制台中上载必要的证书,然后在应用程序中启用推送通知和后台模式->远程通知
在此之后,应用内代理使用以下代码(我指定了棘手的行):
func应用程序(应用程序:UIApplication,didfishlaunchingwithoptions启动选项:[NSObject:AnyObject]?)->Bool{
registerForPushNotifications(应用程序)
//应用程序启动后自定义的覆盖点。
//使用Firebase库配置API
FIRApp.configure()
返回真值
}
func注册表形式通知(应用程序:UIApplication){
让notificationSettings=UIUserNotificationSettings(
forTypes:[.Badge、.Sound、.Alert],类别:无)
application.registerUserNotificationSettings(notificationSettings)
}
func应用程序(应用程序:UIApplication,didRegisterUserNotificationSettings notificationSettings:UIUserNotificationSettings){
如果notificationSettings.types!=.None{
应用程序.注册表项更改()
}
}
func应用程序(应用程序:UIApplication,DidRegisterForRemotionTificationswithDeviceToken deviceToken:NSData){
让tokenChars=UnsafePointer(deviceToken.bytes)
var tokenString=“”
对于0中的i..不要忘记在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.")
}
}
}
请确保在Info.plist中也执行以下操作:FirebaseAppDelegateProxyEnabled=NO
我现在不知道,但我得到了打印(…)
在didReceiveMemotentification
中,但没有弹出窗口。我从Firebase->Console->Notification->Single device发送消息,并在这里复制我从Xcode Console获得的令牌->func TokenRefreshNotification
回答正确这些是步骤,但也请检查您的设备时间。因为如果您的时间和日期太晚,在tripple检查了整个集成后,它将无法工作,对我来说,问题是测试设备将日期更改为提前一周。
FCM SDK可能会执行一些基于日期的检查
这个错误在Firebase方面可能不太常见,因为我花了将近一天的时间寻找解决方案。希望这有帮助。这解决了问题,不再出现错误。现在,当应用程序在后台时,我可以控制数据的下载?@LoGoCSE..我现在也面临同样的问题。缺少的部分:.addObserver
,saved me!@Mr.Bista。请指导我如何使用FCM发送推送通知。我能够从服务器发送数据消息,但通知方法未调用。请检查我的问题您是否正确初始化了通用firebase框架?是的,我的AppDelegate与此示例相同。我正确接收通知,p问题是当我关闭应用程序并重新打开一个崩溃时,然后重试并正常运行。非常感谢,这也是我的问题!
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.")
}
}
}