我正在尝试将Firebase消息传递集成到IOS中。我的目标是在应用程序处于前台和后台时都处理数据消息。
下面的示例主要是从https://firebase.google.com/docs/cloud-messaging/ios/client和一些SO中获得的。
我有以下三种设置Firebase的主要方法。我的两个主要要求是处理令牌更改和处理数据消息。
yield Request(url=response.url,
method='POST',
headers={
'Content-Type': 'application/json; charset=UTF-8',
},
body=json.dumps(self.postRequest),
callback=self.parse_data,
dont_filter=True,
cookies=self.cookies)
从AppDelegate.m'didFinishLaunchingWithOptions'调用initFirebase时,它可以完美运行。令牌已设置。收到消息。
- (void)initFirebase{
[FIRApp configure];
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
printf("\n#B initFirebase.IOS 9");
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:settings];
} else {
// iOS 10 or later
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
printf("\n#B initFirebase.IOS 10+");
UNAuthorizationOptions authOptions =
UNAuthorizationOptionAlert
| UNAuthorizationOptionSound
| UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL granted, NSError * _Nullable error) {
}];
// For iOS 10 display notification (sent via APNS)
[UNUserNotificationCenter currentNotificationCenter].delegate = self;
// For iOS 10 data message (sent via FCM)
[FIRMessaging messaging].delegate = self;
#endif
}
[[UIApplication sharedApplication] registerForRemoteNotifications];
// NSLog(@"#B initFirebase.fir token is %@",[[FIRInstanceID instanceID] token]);
printf("\n#B initFirebase. token: %s", [[[FIRInstanceID instanceID] token] UTF8String]);//token is null here
}
// [START refresh_token]
//called everytime token changes. must register
- (void)messaging:(FIRMessaging *)messaging didReceiveRegistrationToken:(NSString *)fcmToken {
printf("\n#B didReceiveRegistrationToken. token: %s", [fcmToken UTF8String]);
//NSLog(@"FCM registration token: %@", fcmToken);
// Notify about received token.
NSDictionary *dataDict = [NSDictionary dictionaryWithObject:fcmToken forKey:@"token"];
[[NSNotificationCenter defaultCenter] postNotificationName:
@"FCMToken" object:nil userInfo:dataDict];
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}
// [END refresh_token]
//called everytime a message is received. must call custom message handler
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult result))completionHandler{
printf("\n#B application didReceiveRemoteNotification message:");
//NSLog(@"#B received notification");
//handle the notification here
}
但是,我的要求是仅在用户从设置中选择选项时设置Firebase。
在单击按钮后调用“ initFirebase”代码时,仅调用“ didReceiveRegistrationToken”。
完全不接收消息。永远不会调用'didReceiveRemoteNotification'。
是否可以根据用户操作从控制器初始化Firebase?实现此目的的正确方法是什么?