我正在为一个项目使用 Firebase,并且我想使用推送通知。
有没有办法通过代码向我的应用程序的所有用户发送推送通知,即不使用控制台?
我使用 Cloudkit 和 CKSubscritpitons 完成了这项工作,我想知道是否有办法在 Firebase 中做类似的事情。
谢谢!
我正在为一个项目使用 Firebase,并且我想使用推送通知。
有没有办法通过代码向我的应用程序的所有用户发送推送通知,即不使用控制台?
我使用 Cloudkit 和 CKSubscritpitons 完成了这项工作,我想知道是否有办法在 Firebase 中做类似的事情。
谢谢!
通知控制台用于向用户组发送通知。
如果您想以编程方式发送通知,请查看Firebase Cloud Messaging。这将要求您在受信任的进程(例如服务器)中运行代码。
首先阅读 Firebase 文档。=> 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;
@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 {
NSLog(@"InstanceID_connectToFcm = %@", InstanceID);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
[self sendDeviceInfo];
NSLog(@"instanceId_tokenRefreshNotification22=>%@",[[FIRInstanceID instanceID] token]);
});
});
}
}];}