【问题标题】:Firebase notifications programmatically? objective-cFirebase 以编程方式通知?目标-c
【发布时间】:2016-05-25 14:59:19
【问题描述】:

我正在为一个项目使用 Firebase,我想使用推送通知。

有没有办法通过代码向我的应用程序的所有用户发送推送通知,即不使用控制台?

我使用 Cloudkit 和 CKSubscritpitons 完成了这项工作,我想知道是否有办法在 Firebase 中做类似的事情。

谢谢!

【问题讨论】:

  • 这个问题不适合 SO,因为它主要是基于意见的问题。
  • 你有解决办法吗?我猜是 。你能指导我完成吗?因为我也在处理同样的情况。我正在调用 firebase 通过 web-service 发送通知。但我没有得到任何。从firebase手动,我越来越完美。帮我解决这个问题,如果你已经想通了。
  • 嗨 Moxarth,我找到了两个解决这个问题的方法:1) 注册第三方服务,如 OneSignal(非常棒),或 2) 使用 firebase 函数并从您的应用调用 POST 请求使用 FIRMessaging 令牌。

标签: ios objective-c push-notification firebase firebase-notifications


【解决方案1】:

Notifications Console 用于向用户组发送通知。

如果您想以编程方式发送通知,请查看Firebase Cloud Messaging。这将要求您在受信任的进程(例如服务器)中运行代码。

【讨论】:

  • 我已完成所有步骤,但无法理解一个程序。当我手动从firebase发送消息时,我也会收到通知和消息,但是当我调用firebase通过网络服务发送通知时,我没有收到任何通知或任何消息。与 android 相同的场景工作正常。那么对于ios,我应该更多地使用什么以及哪些方法?我是否缺少其他一些功能?请帮我解决这个问题。
【解决方案2】:

无需 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
  9. 在您的 Info.Plist 文件中添加 FirebaseAppDelegateProxyEnabled 设置 BOOL NO

在 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]);

            });
        });


    }
}];}

【讨论】:

  • 我已完成所有步骤,但无法理解一个程序。当我手动从firebase发送消息时,我也会收到通知和消息,但是当我调用firebase通过网络服务发送通知时,我没有收到任何通知或任何消息。与 android 相同的场景工作正常。那么对于ios,我应该更多地使用什么以及哪些方法?我是否缺少其他一些功能?请帮我解决这个问题。
  • 请原谅我的无知,但您介意指出您在代码中的哪个位置通过推送通知发送消息吗?看来是我,您只注册接收推送通知。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-03
  • 1970-01-01
  • 1970-01-01
  • 2017-10-11
  • 2016-07-24
  • 2010-11-30
相关资源
最近更新 更多