【问题标题】:Notification in iOS using new Firebase Messaging SDKiOS 中使用新的 Firebase 消息传递 SDK 的通知
【发布时间】:2016-09-29 04:53:49
【问题描述】:

最近 google 宣布了具有更多功能的新 Firebase SDK。因此,我正在寻找关于如何在 iOS 中使用新的 Firebase 消息 SDK(FCM) 实现远程通知功能的完美文档。

提前非常感谢。

【问题讨论】:

  • 为什么你在回答问题之前不做一些谷歌。 Firebase 提供分步指南 花一些时间学习和阅读它:firebase.google.com/docs/cloud-messaging
  • @NitinGohel 我已经完成了,但这对我不起作用。您有自己的示例项目吗?
  • @NitinGohel 我的应用程序在以下功能第一行崩溃,- (void)connectToFcm { [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) { if (error != nil) { NSLog(@"Unable to connect to FCM. %@", error); } else { NSLog(@"Connected to FCM."); } }]; } 错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'+[NSData gtm_dataByGzippingData:]:无法识别的选择器发送到类 0x10fd1f110' 你有什么建议吗?
  • 你能发布整个堆栈跟踪吗?这似乎是图书馆中的一个错误。另外你可能想在这里提出一张票github.com/google/gcm/issues

标签: ios objective-c iphone notifications firebase


【解决方案1】:

无需 CocoaPods 即可集成

首先阅读 Firebase 文档。 => Firebase Doc

  1. 在此处在 Firebase 上注册项目 => Register Project here

  2. 从这里获取 GoogleService-Info.plist 文件=> 项目=> 设置 => 常规

  3. GoogleService-Info.plist 文件拖放到您的项目中。

  4. 在 Firebase 中设置通知 .p12 证书(生产和开发) => 项目=> 设置 => 云消息传递

  5. 在此处下载 Firebase SDK => Firebase SDK Download

  6. 在您的项目中创建 SDK 文件夹并将所有 SDK 文件夹放入其中。

  7. 现在在你的 Xcode 中添加这个框架 => libicucore.tbd

  8. 在 Xcode 中设置背景模式为 ON => 项目 => 功能 => 背景模式为 ON => RemoteNotification

在 Objective-c 中 你的 Appdelegate.m 文件

#import "AppDelegate.h"
#import "Firebase.h"
#import "AFNHelper.h"

@interface AppDelegate (){
    
    NSString *InstanceID;
}
@property (nonatomic, strong) NSString *strUUID;
@property (nonatomic, strong) NSString *strDeviceToken;
@end
@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    UIUserNotificationType allNotificationTypes =
    (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
    UIUserNotificationSettings *settings =
    [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    [[UIApplication sharedApplication] registerForRemoteNotifications];

    [FIRApp configure];
   
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:) name:kFIRInstanceIDTokenRefreshNotification object:nil];
    
    return YES;
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);
    [[FIRMessaging messaging] appDidReceiveMessage:userInfo];
    
    NSLog(@"userInfo=>%@", userInfo);
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    
    [[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeProd];
    NSLog(@"deviceToken1 = %@",deviceToken);
    
}
- (void)tokenRefreshNotification:(NSNotification *)notification {
   NSLog(@"instanceId_notification=>%@",[notification object]);
    InstanceID = [NSString stringWithFormat:@"%@",[notification object]];
   
 [self connectToFcm];  
}

- (void)connectToFcm {
    
    [[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
        if (error != nil) {
            NSLog(@"Unable to connect to FCM. %@", error);
        } else {
            
            // you can send your token here with api or etc.... 
                   
        }
}

【讨论】:

  • 即使 Firebase 中的示例不需要它,为什么还要添加 didRegisterForRemoteNotificationsWithDeviceToken 函数?示例:github.com/firebase/quickstart-ios
  • 它在 firebase 中是最重要的。在这里,我将设备令牌传递给 Firebase,然后 Firebase 返回令牌。这个 Firebase 令牌可用于向任何一台设备发送推送通知。
  • 我明白了。我不是试图从设备发送通知,而是从通知控制台发送到设备。我不认为我需要那个。 @sanandyavipul
  • 那你的问题是什么? @justColbs
  • 如果我只想从 Firebase 通知控制台向所有设备发送推送通知,是否需要添加该代码?我不想在设备之间发送通知。
猜你喜欢
  • 2020-07-16
  • 2021-02-19
  • 2021-07-22
  • 2019-06-19
  • 2019-02-06
  • 2020-06-16
  • 2017-05-05
  • 1970-01-01
  • 2021-08-21
相关资源
最近更新 更多