2017-06-21 2 views
0

iOSのFCMの通知は、Googleのコンソールから作業ではなく、サーバーiOSのFCM通知Googleのコンソールから作業しますが、サーバーから

からここでは、サーバからの応答がこのフォーマットである私のAppdelegate.m

#import "AppDelegate.h" 
#import <UserNotifications/UserNotifications.h> 

#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 
@import UserNotifications; 
#endif 

// Implement UNUserNotificationCenterDelegate to receive display notification via APNS 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 { 
    [NSThread sleepForTimeInterval:3.0]; 
    self.defaults = [NSUserDefaults standardUserDefaults]; 
    self.selectedIndexArray = [[NSMutableArray alloc]init]; 


    // [START configure_firebase] 
    [FIRApp configure]; 
    // [END configure_firebase] 

    // [START set_messaging_delegate] 
    [FIRMessaging messaging].delegate = self; 
    // [END set_messaging_delegate] 
     // Register for remote notifications. This shows a permission dialog on first run, to 
    // show the dialog at a more appropriate time move this registration accordingly. 
    if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) { 
     // iOS 7.1 or earlier. Disable the deprecation warnings. 
#pragma clang diagnostic push 
#pragma clang diagnostic ignored "-Wdeprecated-declarations" 
     UIRemoteNotificationType allNotificationTypes = 
     (UIRemoteNotificationTypeSound | 
     UIRemoteNotificationTypeAlert | 
     UIRemoteNotificationTypeBadge); 
     [application registerForRemoteNotificationTypes:allNotificationTypes]; 
#pragma clang diagnostic pop 
    } else { 
     // iOS 8 or later 
     // [START register_for_notifications] 
     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 display notification (sent via APNS) 
      [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self]; 
      // For iOS 10 data message (sent via FCM) 
      [[FIRMessaging messaging] setDelegate:self]; 
#endif 
     } 
     [[UIApplication sharedApplication] registerForRemoteNotifications]; 
     // [END register_for_notifications] 
    } 


    return YES; 
} 
// [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 

    // With swizzling disabled you must let Messaging know about the message, for Analytics 
    [[FIRMessaging messaging] appDidReceiveMessage:userInfo]; 

    // 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 

    // With swizzling disabled you must let Messaging know about the message, for Analytics 
    [[FIRMessaging messaging] appDidReceiveMessage:userInfo]; 

    // Print message ID. 
    if (userInfo[kGCMMessageIDKey]) { 
     NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]); 
    } 

    // Print full message. 
    NSLog(@"%@", userInfo); 

    completionHandler(UIBackgroundFetchResultNewData); 
} 


// [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 { 
    NSDictionary *userInfo = notification.request.content.userInfo; 

    // With swizzling disabled you must let Messaging know about the message, for Analytics 
    [[FIRMessaging messaging] appDidReceiveMessage:userInfo]; 

    // Print message ID. 
    if (userInfo[kGCMMessageIDKey]) { 
     NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]); 
    } 

    // Print full message. 
    NSLog(@"%@", userInfo); 
    NSString *notResStr = notification.request.content.body; 
    self.notificationArray = [notResStr componentsSeparatedByString:@","]; 


    // Change this to your preferred presentation option 
    completionHandler(UNNotificationPresentationOptionAlert); 
} 
// 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 : %@", userInfo); 

    completionHandler(UNNotificationPresentationOptionAlert); 
} 
#endif 
// [END ios_10_message_handling] 

// [START refresh_token] 
- (void)messaging:(nonnull FIRMessaging *)messaging didRefreshRegistrationToken:(nonnull NSString *)fcmToken { 
    // 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. 
    NSLog(@"FCM registration token: %@", fcmToken); 
    [self.defaults setObject:fcmToken forKey:@"deviceToken"]; 

    // TODO: If necessary send token to application server. 
} 
// [END refresh_token] 

// [START ios_10_data_message] 
// Receive data messages on iOS 10+ directly from FCM (bypassing APNs) when the app is in the foreground. 
// To enable direct data messages, you can set [Messaging messaging].shouldEstablishDirectChannel to YES. 
- (void)messaging:(FIRMessaging *)messaging didReceiveMessage:(FIRMessagingRemoteMessage *)remoteMessage { 
    NSLog(@"Received data message: %@", remoteMessage.appData); 
} 
// [END ios_10_data_message] 

- (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 device token can be paired to 
// the FCM registration token. 
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
    NSLog(@"APNs device token retrieved: %@", deviceToken); 

    // With swizzling disabled you must set the APNs device token here. 
    [FIRMessaging messaging].APNSToken = deviceToken; 
} 


/*- (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 

    // With swizzling disabled you must let Messaging know about the message, for Analytics 
    // [[Messaging messaging] appDidReceiveMessage:userInfo]; 

    // Print message ID. 
    if (userInfo[kGCMMessageIDKey]) { 
     NSLog(@"Message ID: %@", userInfo[kGCMMessageIDKey]); 
    } 

    // Print full message. 
    NSLog(@"%@", userInfo); 

    completionHandler(UIBackgroundFetchResultNewData); 
}*/ 
#pragma mark - 
#pragma mark Shared Instance 

+ (AppDelegate*) sharedInstance{ 

    return (AppDelegate*)[[UIApplication sharedApplication] delegate]; 

} 
- (void)applicationWillResignActive:(UIApplication *)application { 
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
} 

- (void)applicationDidEnterBackground:(UIApplication *)application { 

} 

- (void)applicationWillEnterForeground:(UIApplication *)application { 
} 
- (void)applicationDidBecomeActive:(UIApplication *)application { 
} 
- (void)applicationWillTerminate:(UIApplication *)application { 
    [self.defaults synchronize]; 

} 

@end 

ありません

Array 
(
    [to] => ..... 
    [aps] => Array 
     (
      [data] => Test silent background push 
      [moredata] => Do more stuff 
     ) 

    [priority] => high 
    [notification] => Array 
     (
      [body] => Test Body 
      [title] => Test Title 
     ) 

    [data] => Array 
     (
      [Nick] => Mario 
      [Room] => PortugalVSDenmark 
     ) 

) 

通知キーがあり、優先度も高く設定されていますが、メソッド自体はフォアグラウンドとバックグラウンドに関係なく呼び出されません。 FCM通知を取得するためにコードを変更する必要がありますか?

異なるデバイスで試行

もフォアグランドとバックグランド

data = {image:\"\/logo.png\", 
     notification:{\"themeicon\":\"public\\/upload_files\\/theme\\/events.png\", 
         "notification_type\":\"Mee\", 
         \"mob_number\ ":\"99999999999\", 
         \"shoutouttitle\ ":\"test\", 
         \"themecolor\ ":\"#6f95f5\", 
         \"theme_Id\ ":\"3\", 
         \"student_Id\ ":\"9\", 
         \"shoutoutuploadpath\ ":\"\",\"notification_Id\ ":\"107\", 
         \"shoutoutmessage\ ":\"test msg\", 
         \"shoutouttimeslot\ ":\"\" 
        }, 
    \"is_background\ ":false, 
    \"payload\ ":{\"score\":\"5.6\", 
        \"team\ ":\"India\" 
       }, 
    \"title\":\Events\", 
    \"message\":\"test\", 
    \"timestamp\":\"2017-06-09 8:59:45\" 
}";from = 608855029366; 
} 

しかしで、応答を受信するAndroid用NO

$url = 'https://fcm.googleapis.com/fcm/send'; 
     $headers = array(
      'Authorization: key=' . FIREBASE_API_KEY, 
      'Content-Type: application/json' 
     ); 
      $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_POST, true); 
     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 
     // Execute post 
     $result = curl_exec($ch); 

へのInfo.plistにfirebaseproxyenabledセットiOSはアプリの状態に関係なく何も受信していません

通知がGoogleのコンソールから届いたときに通知を受信する

+0

次のメッセージとサンプルペイロードを送信するためのサーバー側のコードを投稿してもらえますか? –

+0

@AL。サーバー側コード$ url = 'https://fcm.googleapis.com/fcm/send'; $ headers = array( '認証:key ='。FIREBASE_API_KEY、 'Content-Type:application/json' ); $ ch = curl_init(); curl_setopt($ ch、CURLOPT_URL、$ url); curl_setopt($ ch、CURLOPT_POST、true); curl_setopt($ ch、CURLOPT_HTTPHEADER、$ headers); curl_setopt($ ch、CURLOPT_RETURNTRANSFER、true); curl_setopt($ ch、CURLOPT_SSL_VERIFYPEER、false); curl_setopt($ ch、CURLOPT_POSTFIELDS、json_encode($ fields)); //投稿を実行する$ result = curl_exec($ ch); – UseriOSDev

+0

とサンプルのペイロードが質問 – UseriOSDev

答えて

4

問題は、あなたが得ているペイロードフォーマットのためである必要があります。

それは形式以下のようにする必要があります: -

{ 
    "aps" : { 
     "alert" : { 
     "body" : "great match!", 
     "title" : "Portugal vs. Denmark", 
     }, 
     "badge" : 1, 
    }, 
    "customKey" : "customValue" 
    } 
+0

こんにちは、同じ応答で試してもまだ運がないと優先度とcontent_availableキーを追加しました – UseriOSDev

+0

プロジェクトの設定でリモート通知のバックグラウンドモードを有効にしてください。 –

+0

有効になっている背景モードをチェックしました。リモート通知がチェックされます – UseriOSDev

関連する問題