firebase通知,立即启动和关闭ios应用程序
使用Firebase发送IOS通知。我点击我的iphone上的通知消息,它启动应用程序并立即关闭应用程序。我不知道我做错了什么,你能帮忙解决这个问题吗?firebase通知,立即启动和关闭ios应用程序
在这里与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 {
//add it for firebase
FIRApp.configure()
let notificationTypes : UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Badge, UIUserNotificationType.Sound]
let notificationSettings = UIUserNotificationSettings(forTypes: notificationTypes, categories: nil)
application.registerForRemoteNotifications()
application.registerUserNotificationSettings(notificationSettings)
// Override point for customization after application launch.
return true
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
print("MessgaeID : \(userInfo["gcm_message_id"]!)")
print(userInfo)
}
}
2016年9月17日00:25:35.709调解[922:20184]配置默认应用。 2016-09-17 00:25:35.724调解[922:] Firebase Analytics v.3402000开始 2016-09-17 00:25:35.725调解[922:]要启用调试日志记录,请设置以下应用程序参数:-FIRAnalyticsDebugEnabled 2016-09-17 00:25:35.729:FIRInstanceID启用了AppDelegate代理,将swizzle应用委托给远程通知处理程序。要禁用将“FirebaseAppDelegateProxyEnabled”添加到Info.plist并将其设置为NO 2016-09-17 00:25:35.729:无法获取APNS令牌Error Domain = com.firebase.iid Code = 1001“(null)” 2016-09-17 00:25:35.731:FIRMessaging库版本1.2.0 2016-09-17 00:25:35.734:FIRMessaging AppDelegate代理启用后,将调试应用委托远程通知接收处理程序。将“FirebaseAppDelegateProxyEnabled”添加到Info.plist并将其设置为NO 2016-09-17 00:25:35.752中介[922:]自动成功创建Firebase Analytics应用程序委托代理。要禁用代理,请将Info.plist中的标志FirebaseAppDelegateProxyEnabled设置为NO 2016-09-17 00:25:35.785调解[922:]启用了Firebase Analytics 2016-09-17 00:25:35.813:无法提取APNS令牌错误域= NSCocoaErrorDomain代码= 3010 “REMOTE_NOTIFICATION_SIMULATOR_NOT_SUPPORTED_NSERROR_DESCRIPTION” 的UserInfo = {NSLocalizedDescription = REMOTE_NOTIFICATION_SIMULATOR_NOT_SUPPORTED_NSERROR_DESCRIPTION}
虽然我个人认为你的错误可能归因于的误拼写代码中的“MESSAGEID”:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
print("MessgaeID : \(userInfo["gcm_message_id"]!)")
print(userInfo)
}
在不发生是问题的情况下,试试这个:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// Let FCM know about the message for analytics
FIRMessaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
print("Message ID: \(userInfo["gcm.message_id"]!)")
// Print full message.
print("%@", userInfo)
}
在任何情况下,你的的AppDelegate应该看起来像下面这样:
import UIKit
import Firebase
import FirebaseInstanceID
import FirebaseMessaging
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var alertTitle: String?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Continue to play users music from iTunes/Apple Music/ third party music streaming services
try! AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryAmbient)
// Register for remote notifications
if #available(iOS 8.0, *) {
// [START register_for_notifications]
let settings: UIUserNotificationSettings =
UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil)
application.registerUserNotificationSettings(settings)
application.registerForRemoteNotifications()
// [END register_for_notifications]
} else {
// Fallback
let types: UIRemoteNotificationType = [.Alert, .Badge, .Sound]
application.registerForRemoteNotificationTypes(types)
}
FIRApp.configure()
// Add observer for InstanceID token refresh callback.
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.tokenRefreshNotification),
name: kFIRInstanceIDTokenRefreshNotification, object: nil)
return true
}
func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
print("didRegisterForRemoteNotificationsWithDeviceToken()")
// if FirebaseAppDelegateProxyEnabled === NO:
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: .Sandbox)
print("APNS: <\(deviceToken)>")
}
func application(application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: NSError) {
print("Registration for remote notification failed with error: \(error.localizedDescription)")
}
// [START receive_message]
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject],
fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// Let FCM know about the message for analytics
FIRMessaging.messaging().appDidReceiveMessage(userInfo)
// Print message ID.
print("Message ID: \(userInfo["gcm.message_id"]!)")
// Print full message.
print("%@", userInfo)
}
// [END receive_message]
// [START refresh_token]
func tokenRefreshNotification(notification: NSNotification) {
if let refreshedToken = FIRInstanceID.instanceID().token() {
print("InstanceID token: \(refreshedToken)")
}
// Connect to FCM since connection may have failed when attempted before having a token.
connectToFcm()
}
// [END refresh_token]
// [START connect_to_fcm]
func connectToFcm() {
FIRMessaging.messaging().connectWithCompletion { (error) in
if (error != nil) {
print("Unable to connect with FCM. \(error)")
} else {
print("Connected to FCM.")
}
}
}
// [END connect_to_fcm]
func applicationDidBecomeActive(application: UIApplication) {
connectToFcm()
}
// [START disconnect_from_fcm]
func applicationDidEnterBackground(application: UIApplication) {
FIRMessaging.messaging().disconnect()
print("Disconnected from FCM.")
}
func applicationWillEnterForeground(application: UIApplication) {
NSNotificationCenter.defaultCenter().postNotificationName("continueVideo", object: nil)
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application temrinates.
}
}
要么工作。我也使用FCM从我的火力地堡的控制台实现我的应用程序远程推送通知和我使用火力地堡版本3.5.2,这完全适用于我,因为事实上,我只是测试了然后再将它运行到我的设备中,我可以收到通知,轻扫并重新导入应用。
我觉得你的gcm_message_id不存在! gcm_message_id在userInfo中!
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
print("MessgaeID : \(userInfo["gcm_message_id"]!)")
print(userInfo)
}
尝试打印您的userInfo以显示所有您收到的数据。 :)
请让我知道如何打印userinfo –
请让我知道如何打印userinfo –
模拟器无法收到通知,我只能在真正的iphone机器收到通知。 –
你能发布完整的错误信息吗?另外,首先打印出userinfo,我猜gcm_message_id键不存在。 – ohr
请参阅错误消息 –
请让我知道如何打印userinfo –