关于firebase 云推送 关于ios 的坑
这里先按firebase的官方文件接入一遍 link
接入之后测试发现 怎么都接受不到需要的Token firebase的 云消息后台也没有新设备进入的显示
没办法就翻看了各种文档 各种尝试 百般折磨 终于解决。。。
这里要先主要一下的是 要记住需要到ios的开发者后台创建一个apns文件,上传到firebase的后台
创建过程比较简单跳过ios开发 link后台后点击keys ,然后点+号 取一个名字 勾选apns 就可以了,下载下来后是一个p8格式的文件,然再打开firebase的控制台 点击齿轮按钮 然后project setting 然后选择Colud Messaging 然后找到你对应的ios项目 上传apns证书 这里点击上传后需要你输入两个值,keyid就是你刚刚创建apns的文件就可以看到 ,TeamId就是点击identifiers 然后点击你接入项目的id 里面的App id prefix 的id 到此apns的证书问题解决了
现在说说我的解决办法 就是去看firebase关于ios的接入文档 把里面需要的信息摘取出来后就可以了
找到UnityAppController.mm的文件
先导入头文件
#include "FirebaseCore/FIRApp.h"
#include "FirebaseMessaging/FIRMessaging.h"
#include "UserNotifications/UNUserNotificationCenter.h"
#include "FirebaseInstanceID/FIRInstanceID.h"
然后再找到 startUnity 方法 加入
[FIRApp configure];
// [FIRMessaging messaging].delegate = [application messaging];
if ([UNUserNotificationCenter class] != nil) {
// iOS 10 or later
// For iOS 10 display notification (sent via APNS)
// [UNUserNotificationCenter currentNotificationCenter].delegate = self;
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// ...
}];
} else {
// iOS 10 notifications aren't available; fall back to iOS 8-9 notifications.
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
}
[application registerForRemoteNotifications];
[[FIRInstanceID instanceID] instanceIDWithHandler:^(FIRInstanceIDResult * _Nullable result,
NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Error fetching remote instance ID: %@", error);
} else {
NSLog(@"Remote instance ID token: %@", result.token);
NSString* message =
[NSString stringWithFormat:@"Remote InstanceID token: %@", result.token];
//self.instanceIDTokenMessage.text = message;
}
}];
现在再去测试就发现就可以接受到Token了,拿着这个Token就可以取后台做测试了
这代码其中这一块
if ([UNUserNotificationCenter class] != nil) {
// iOS 10 or later
// For iOS 10 display notification (sent via APNS)
// [UNUserNotificationCenter currentNotificationCenter].delegate = self;
UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
[[UNUserNotificationCenter currentNotificationCenter]
requestAuthorizationWithOptions:authOptions
completionHandler:^(BOOL granted, NSError * _Nullable error) {
// ...
}];
} else {
// iOS 10 notifications aren't available; fall back to iOS 8-9 notifications.
UIUserNotificationType allNotificationTypes =
(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
UIUserNotificationSettings *settings =
[UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
[application registerUserNotificationSettings:settings];
}
如果不想加也可以省去 省去后测试发现还是有效果的