- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
最近在 Google I/O 事件上,Google 更新了 Firebase 并添加了许多新功能,并对其余功能进行了修改。我一直在尝试通过最基本的级别通过 Firebase 将 iOS 推送通知实现到我的应用程序中,所以我创建了一个非常简单的应用程序,除了接收远程推送通知之外什么都不做。
在 Firebase 中,我上传了我的证书,在 Xcode 中,我的配置文件已添加到目标和项目中,在 Firebase 中,我上传了正确的证书。下面是包含在我的 AppDelegate.swift
文件中的代码,但是因为我的 ViewController.swift
是“空的”,所以我没有包含它。
虽然没有崩溃或运行时错误,但当我加载应用程序时,我接受了通知。然后,我退出应用程序并关闭我的设备。在 Firebase 中,我将通知发送到正确的应用程序。几分钟后,在 Firebase 中显示通知“已完成”。
但是,我从未在设备上收到通知。因此,总而言之,我需要一个解决方案来向 Firebase 发送此 deviceToken
,然后使用“Firebase Notifications”发送推送通知消息。
任何对我的代码或一般情况的帮助都将不胜感激,我希望这对 future 的观众有所帮助。谢谢!我在 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)
}
}
最佳答案
更新:从 Firebase 4.0.4 开始,您可以关注 https://github.com/onmyway133/blog/issues/64
如何处理 APNS 设备 token
我一直在阅读 Send a Notification to a User Segment on iOS但是没有提到对推送通知至关重要的 APNS 设备 token 。
因此,Firebase 一定在进行一些内部调整。事实上它是。阅读后端文档 Downstream Messages给了我们想法
Swizzling disabled: mapping your APNs token and registration token
If you have disabled method swizzling, you'll need to explicitly map your APNs token to the FCM registration token. Override the
methods
didRegisterForRemoteNotificationsWithDeviceToken
to retrieve the APNs token, and then callsetAPNSToken
.
func application(application: UIApplication,
didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenTypeSandbox)
}
我特别尝试尽可能避免混合。读书Migrate a GCM Client App for iOS to Firebase Cloud Messaging告诉我们如何禁用它
启用/禁用方法调配
Method swizzling available with FCM simplifies your client code. However, for developers who prefer not to use it, FCM allows you to disable method swizzling by adding the FIRMessagingAutoRegisterEnabledflag in the app’s Info.plist file and setting its value to NO (boolean value).
FCM swizzling affects how you handle the default registration token, and how you handle downstream message callbacks. Where
applicable, this guide provides migration examples both with and without method swizzling enabled.
给我看代码
将其放入您的 Podfile
pod 'Firebase'
pod 'FirebaseMessaging'
这里是完整的代码
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 to use this when swizzling is disabled
public func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) {
FIRInstanceID.instanceID().setAPNSToken(deviceToken, type: FIRInstanceIDAPNSTokenType.Sandbox)
}
func tokenRefreshNotification(notification: NSNotification) {
// NOTE: It can be nil here
let refreshedToken = FIRInstanceID.instanceID().token()
print("InstanceID token: \(refreshedToken)")
connectToFcm()
}
func connectToFcm() {
FIRMessaging.messaging().connectWithCompletion { (error) in
if (error != nil) {
print("Unable to connect with FCM. \(error)")
} else {
print("Connected to FCM.")
}
}
}
public func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
print(userInfo)
}
关于iOS Firebase 推送通知 : How To Give Firebase User's Device Token And Send Notification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37667753/
编译器知道AbstractDemo是一个抽象类,抽象类不能被实例化。 但是当我调用 newInstance() 方法时,为什么它没有给出编译时错误? import java.lang.reflect.
假设我有如下数据类型: data Cell = Cell (Maybe Player) data Board = Board [[Cell]] 现在我想生成一个这样的递归函数: genBoard
当谈到使用 OpenMP 和 TBB 进行共享内存编程时,我是一个初学者。 我正在实现 QuickHull 算法 ( http://en.wikipedia.org/wiki/QuickHull )
我想创建一个随机列表与列表中元素的不同组合。出现在最终列表中的每个元素的计数应该相同。。例如。在这里,我期望所有元素的计数都是10,因为我给了相同的权重。但这并不总是一样的。如何修改代码以使所有元素的
我想创建一个随机列表与列表中元素的不同组合。出现在最终列表中的每个元素的计数应该相同。。例如。在这里,我期望所有元素的计数都是10,因为我给了相同的权重。但这并不总是一样的。如何修改代码以使所有元素的
以下将显示在 Firebug 或 jsconsole.com 中或在其他 Javascript 交互式控制台中: >>> foo = { a : 1, b : 2.2 } Object { a=1,
在 Azure DevOps 管道中的一项任务中,我尝试停止 IIS 服务器。这可以通过在命令提示符中调用命令net stop WAS来实现。手动执行此操作,它会要求确认 手动方式,我只需按 Y EN
在 Azure DevOps 管道中的一项任务中,我尝试停止 IIS 服务器。这可以通过在命令提示符中调用命令net stop WAS来实现。手动执行此操作,它会要求确认 手动方式,我只需按 Y EN
在 R 编码中出现以下错误。 在我的 Brand_X.xlsx 数据集中,我尝试使用 KNN 插补法计算的 NA 值很少,但我得到的结果低于错误。这里有什么问题吗?谢谢! > library(read
在 Android Studio 中,我希望我的每个应用变体都有自己的图标。 我尝试了各种方法,包括此处建议的方法 How to provide different Android app icons
JSFiddle 示例代码:http://jsfiddle.net/SUMPq/8/ 我在容器上有一些文本,代码位于 HTML 的顶部,靠近正文: Heading TAG SOME TEXTSOM
所以我正在实现这个简单的剃须刀支付集成。但它给我一个“没有找到合适的付款方式”的错误。我之前尝试过选择付款选项表,但也没有用。 val razorpay = RazorpayClient("my
Closed. This question needs to be more focused。它当前不接受答案。 想要改善这个问题吗?更新问题,使它仅关注editing this post的一个问题。
我在玩 elm-css . 大多数事情都按我的预期工作。 但是我无法为 Css.opacity 提供正确的值功能。 这是我尝试过的: Css.opacity 0.5 这给出了错误: Function
请参阅以下代码: UIImage *image; NSString *str = [[[Data getInstance]arrPic]objectAtIndex:rowIndex]; NSLog(s
这个问题已经有答案了: "Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key",
我正在测试一个用于修改文件的工具,在此过程中一个相当重要的功能是告诉文件大小,尤其是当文件仍然打开时。 $file = tempnam('/tmp', 'test_'); file_put_conte
我是Java/Maven新手,我正在尝试构建一个maven Spring Boot项目,它很早就可以工作并且也成功创建了jar包。但它突然停止工作并开始出现 Maven 编译错误。 我知道它与 pom
我正在尝试让我的 pom.xml 在我的 JAXB 对象上生成 hashCode() 和 equals method()。 4.0.0 0.0.1-SNAPSHOT jar
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 8 年前。 Improv
我是一名优秀的程序员,十分优秀!