instanceID在实际设备中返回nil,但不是';ios中模拟器中的t-nil
我正在使用fcm进行推送通知。 我在我的项目上完全开发了FCM,我一步一步地遵循firebase教程 (我的语言是客观的-c) 我的设备令牌已生成,但instanceID未生成!!换句话说,refreshToken方法从未调用过!。 现在,当我在模拟器中运行我的项目时,instanceID被生成。但当我在真实设备instanceID中运行我的项目时,返回null!! 我的实时设备中的时间和日期是最新的,但仍然无法工作!!它是在模拟器中运行project时我的控制台的日志: 在真实设备中运行我的项目时,会显示以下日志:instanceID在实际设备中返回nil,但不是';ios中模拟器中的t-nil,ios,objective-c,null,Ios,Objective C,Null,我正在使用fcm进行推送通知。 我在我的项目上完全开发了FCM,我一步一步地遵循firebase教程 (我的语言是客观的-c) 我的设备令牌已生成,但instanceID未生成!!换句话说,refreshToken方法从未调用过!。 现在,当我在模拟器中运行我的项目时,instanceID被生成。但当我在真实设备instanceID中运行我的项目时,返回null!! 我的实时设备中的时间和日期是最新的,但仍然无法工作!!它是在模拟器中运行project时我的控制台的日志: 在真实设备中运行我的
2017-04-12 16:53:44.504 pin-Go[1031] <Notice> [Firebase/Analytics][I-ACS023012] Firebase Analytics enabled
2017-04-12 16:53:44.546831+0430 pin-Go[1031:175957] APNs token retrieved: <f73de751 b71d59b8 f1e3b83a 826646ba d6338e7c a1a062be 4adda159 83cb5fa5>
2017-04-12 16:53:44.804239+0430 pin-Go[1031:175957] animating is complited ..
2017-04-12 16:53:44.804639+0430 pin-Go[1031:175957] animating is complited.
2017-04-12 16:53:45.124628+0430 pin-Go[1031:175957] animating1 ...
2017-04-12 16:53:45.751638+0430 pin-Go[1031:175957] Connected to FCM.
2017-04-12 16:53:45.755046+0430 pin-Go[1031:175957] InstanceID token: (null)
2017-04-12 16:53:44.504 pin Go[1031][Firebase/Analytics][I-ACS023012]已启用Firebase分析功能
2017-04-12 16:53:44.546831+0430 pin Go[1031:175957]检索到的APNs令牌:
2017-04-12 16:53:44.804239+0430插脚[1031:175957]动画制作已完成。。
2017-04-12 16:53:44.804639+0430插脚[1031:175957]动画制作已完成。
2017-04-12 16:53:45.124628+0430插针进入[1031:175957]动画1。。。
2017-04-12 16:53:45.751638+0430引脚Go[1031:175957]连接到FCM。
2017-04-12 16:53:45.755046+0430 pin Go[1031:175957]实例ID令牌:(空)
我的代码与此代码完全相同:
我的日志没有显示任何错误。。。
有人能帮我吗
编辑:这是我的代码:
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
@import UserNotifications;
#endif
@import GoogleMaps;
@import GooglePlaces;
#import "AfzayeshMojodiViewController.h"
#import "ChangeInfoViewController.h"
#import "MasirMontakhaViewController.h"
@import Firebase;
// Implement UNUserNotificationCenterDelegate to receive display notification via APNS for devices
// running iOS 10 and above. Implement FIRMessagingDelegate to receive data message via FCM for
// devices running iOS 10 and above.
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
@interface AppDelegate () <UNUserNotificationCenterDelegate, FIRMessagingDelegate>
@end
#endif
// Copied from Apple's header in case it is missing in some cases (e.g. pre-Xcode 8 builds).
#ifndef NSFoundationVersionNumber_iOS_9_x_Max
#define NSFoundationVersionNumber_iOS_9_x_Max 1299
#endif
@implementation AppDelegate
NSString *const kGCMMessageIDKey = @"gcm.message_id";
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.urlStrWebService = [[NSString alloc] init];
//Start of GCM
// [START configure_firebase]
[FIRApp configure];
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_9_x_Max) {
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
// 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) {
}];
// For iOS 10 data message (sent via FCM)
[FIRMessaging messaging].remoteMessageDelegate = self;
#endif
}
[[UIApplication sharedApplication] registerForRemoteNotifications];
return YES;
}
//start implementing notification reciver :
//// [START receive_message]
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// Print message ID.
if (userInfo[kGCMMessageIDKey]) {
NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
}
// Print full message.
NSLog(@"%@", userInfo);
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// If you are receiving a notification message while your app is in the background,
// this callback will not be fired till the user taps on the notification launching the application.
// TODO: Handle data of notification
// Print message ID.
if (userInfo[kGCMMessageIDKey]) {
NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
}
// Print full message.
NSLog(@"%@", userInfo);
completionHandler(UIBackgroundFetchResultNewData);
}
// [END receive_message]
// [START ios_10_message_handling]
// Receive displayed notifications for iOS 10 devices.
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
// Handle incoming notification messages while app is in the foreground.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
// Print message ID.
NSDictionary *userInfo = notification.request.content.userInfo;
if (userInfo[kGCMMessageIDKey]) {
NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
}
// Print full message.
NSLog(@"%@", userInfo);
// Change this to your preferred presentation option
completionHandler(UNNotificationPresentationOptionNone);
}
// Handle notification messages after display notification is tapped by the user.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)())completionHandler {
NSDictionary *userInfo = response.notification.request.content.userInfo;
if (userInfo[kGCMMessageIDKey]) {
NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]);
}
// Print full message.
NSLog(@"%@", userInfo);
completionHandler();
}
#endif
// [END ios_10_message_handling]
// [START ios_10_data_message_handling]
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
// Receive data message on iOS 10 devices while app is in the foreground.
- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage {
// Print full message
NSLog(@"%@", remoteMessage.appData);
}
#endif
// [END ios_10_data_message_handling]
// [START refresh_token]
- (void)tokenRefreshNotification:(NSNotification *)notification {
// Note that this callback will be fired everytime a new token is generated, including the first
// time. So if you need to retrieve the token as soon as it is available this is where that
// should be done.
NSString *refreshedToken = [[FIRInstanceID instanceID] token];
NSLog(@"InstanceID token: %@", refreshedToken);
// Connect to FCM since connection may have failed when attempted before having a token.
[self connectToFcm];
// TODO: If necessary send token to application server.
}
// [END refresh_token]
// [START connect_to_fcm]
- (void)connectToFcm {
// // Won't connect since there is no token
// if (![[FIRInstanceID instanceID] token]) {
// return;
// }
// Disconnect previous FCM connection if it exists.
[[FIRMessaging messaging] disconnect];
[[FIRMessaging messaging] connectWithCompletion:^(NSError * _Nullable error) {
if (error != nil) {
NSLog(@"Unable to connect to FCM. %@", error);
} else {
NSLog(@"Connected to FCM.");
NSString *refreshedToken = [[FIRInstanceID instanceID] token];
NSLog(@"InstanceID token: %@", refreshedToken);
}
}];
}
// [END connect_to_fcm]
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
NSLog(@"Unable to register for remote notifications: %@", error);
}
// This function is added here only for debugging purposes, and can be removed if swizzling is enabled.
// If swizzling is disabled then this function must be implemented so that the APNs token can be paired to
// the InstanceID token.
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
NSLog(@"APNs token retrieved: %@", deviceToken);
// With swizzling disabled you must set the APNs token here.
[[FIRInstanceID instanceID] setAPNSToken:deviceToken type:FIRInstanceIDAPNSTokenTypeSandbox];
[self connectToFcm];
}
// [START connect_on_active]
- (void)applicationDidBecomeActive:(UIApplication *)application {
[self connectToFcm];
}
// [END connect_on_active]
// [START disconnect_from_fcm]
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[FIRMessaging messaging] disconnect];
NSLog(@"Disconnected from FCM");
}
// [END disconnect_from_fcm]
//end of implement notification.
#如果定义(uu IPHONE_10_0)和&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
@导入用户通知;
#恩迪夫
@导入谷歌地图;
@输入谷歌网站;
#导入“AfzayeshMojodiViewController.h”
#导入“ChangeInfoViewController.h”
#导入“MasirMontakhaViewController.h”
@进口火基;
//实现UnuseNotificationCenterDelegate以通过APN接收设备的显示通知
//运行iOS 10及以上版本。实施FIRMessagingDelegate以通过FCM接收数据消息
//运行iOS 10及以上版本的设备。
#如果已定义(uuIphone_10_0)&&&uIphone_OS_0版本允许的最大值>=\uIphone_10_0
@接口AppDelegate()
@结束
#恩迪夫
//复制自苹果的标题,以防在某些情况下丢失(例如,Xcode 8之前的版本)。
#ifndef NSFoundationVersionNumber\u iOS\u 9\u x\u Max
#定义NSFoundationVersionNumber\u iOS\u 9\u x\u Max 1299
#恩迪夫
@实现AppDelegate
NSString*const kGCMMessageIDKey=@“gcm.message_id”;
-(BOOL)应用程序:(UIApplication*)应用程序使用选项完成启动:(NSDictionary*)启动选项{
self.urlStrWebService=[[NSString alloc]init];
//GCM的开始
//[启动配置\u firebase]
[FIRApp配置];
如果(楼层(NSFoundationVersionNumber)=\uu IPHONE\u10\u0
//对于iOS 10显示通知(通过APNS发送)
[UnuseNotificationCenter currentNotificationCenter]。委托=自身;
未授权选项授权选项=
未授权选项ERT
|未授权选项声音
|未经授权的选择徽章;
[[UNUserNotificationCenter currentNotificationCenter]requestAuthorizationWithOptions:authOptions completionHandler:^(BOOL-grated,NSError*_-Nullable-error){
}];
//对于iOS 10数据消息(通过FCM发送)
[FIRMessaging messaging].remoteMessageDelegate=self;
#恩迪夫
}
[[UIApplication sharedApplication]注册表项的删除];
返回YES;
}
//开始实施通知接收器:
////[启动接收消息]
-(无效)应用程序:(UIApplication*)应用程序DidReceiveMemotentification:(NSDictionary*)用户信息{
//如果您在应用程序处于后台时收到通知消息,
//在用户点击启动应用程序的通知之前,不会触发此回调。
//TODO:处理通知的数据
//打印消息ID。
if(userInfo[kGCMMessageIDKey]){
NSLog(@“消息ID:%@”,userInfo[kGCMMessageIDKey]);
}
//打印完整消息。
NSLog(@“%@”,userInfo);
}
-(无效)应用程序:(UIApplication*)应用程序DidReceiveMemotentification:(NSDictionary*)用户信息
fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler{
//如果您在应用程序处于后台时收到通知消息,
//在用户点击启动应用程序的通知之前,不会触发此回调。
//TODO:处理通知的数据
//打印消息ID。
if(userInfo[kGCMMessageIDKey]){
NSLog(@“消息ID:%@”,userInfo[kGCMMessageIDKey]);
}
//打印完整消息。
NSLog(@“%@”,userInfo);
completionHandler(UIBackgroundFetchResultNewData);
}
//[结束接收消息]
//[启动ios\u 10\u消息\u处理]
//接收iOS 10设备的显示通知。
#如果已定义(uuIphone_10_0)&&&uIphone_OS_0版本允许的最大值>=\uIphone_10_0
//在应用程序处于前台时处理传入的通知消息。
-(void)用户通知中心:(UNUserNotificationCenter*)中心
遗嘱提示通知:(未通知*)通知
withCompletionHandler:(void(^)(UNNotificationPresentationOptions))completionHandler{
//打印消息ID。
NSDictionary*userInfo=notification.request.content.userInfo;
if(userInfo[kGCMMessageIDKey]){
NSLog(@“消息ID:%@”,userInfo[kGCMMessageIDKey]);
}
//打印完整消息。
NSLog(@“%@”,userInfo);
//将此更改为首选的演示选项
completionHandler(UNNotificationPresentationOptionNone);
}
//在用户点击显示通知后处理通知消息。
-(void)用户通知中心:(UNUserNotificationCenter*)中心
didReceiveNotificationResponse:(未通知响应*)响应
withCompletionHandler:(void(^)()completionHandler{
NSDictionary*userInfo=response.notification.request.content.userInfo;
if(userInfo[kGCMMessageIDKey]){
NS