在 iOS 中使用新的 Firebase Messaging SDK 进行通知 [英] Notification in iOS using new Firebase Messaging SDK

查看:12
本文介绍了在 iOS 中使用新的 Firebase Messaging SDK 进行通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

非常感谢.

解决方案

无需 CocoaPods 的集成

首次阅读 Firebase 文档. =>

在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;@结尾@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {UIUserNotificationType allNotificationTypes =(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);UIUserNotificationSettings *settings =[UIUserNotificationSettings settingsForTypes:allNotificationTypes 类别:nil];[[UIApplication sharedApplication] registerUserNotificationSettings:settings];[[UIApplication sharedApplication] registerForRemoteNotifications];【FIRApp配置】;[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tokenRefreshNotification:) name:kFIRInstanceIDTokenRefreshNotification object:nil];返回是;}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfofetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {NSLog(@"消息 ID: %@", userInfo[@"gcm.message_id"]);[[FIRMessaging 消息] 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=>%@",[通知对象]);InstanceID = [NSString stringWithFormat:@"%@",[通知对象]];[自连接到Fcm];}- (void)connectToFcm {[[FIRMessaging 消息] connectWithCompletion:^(NSError * _Nullable 错误) {如果(错误!= nil){NSLog(@"无法连接FCM.%@", error);} 别的 {//您可以使用 api 等将您的令牌发送到这里....}}

Recently google announce the new Firebase SDK with lot more feature. So, I'm looking for the perfect documentation of How to implement Remote Notification feature using new Firebase Messaging SDK(FCM) in iOS.

Thanks a lot in advance.

解决方案

Integrate without CocoaPods

First Read Firebase Doc. => Firebase Doc

  1. Register project on Firebase here => Register Project here

  2. Get GoogleService-Info.plist file from here=> project=> settings => General

  3. GoogleService-Info.plist file drop in your project.

  4. set Notification .p12 Certificates (Production and Development) in Firebase => project=> settings => Cloud Messaging

  5. Download Firebase SDK here => Firebase SDK Download

  6. Create SDK folder in your project and Drop all SDK Folder in it.

  7. Now Add this Framework in your Xcode => libicucore.tbd

  8. Set Background Modes ON in Xcode => Projects => Capabilities => Background Mode ON => RemoteNotification

In Objective-c your Appdelegate.m file

#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.... 
                   
        }
}

这篇关于在 iOS 中使用新的 Firebase Messaging SDK 进行通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆