2017-04-18 134 views
1

這是我發送的json,但我只在IOS的前臺接收通知,但不在後臺。我在IOS上沒有在後臺接收fcm推送通知

remoteMessage.appData {          colorCode = XXXXX;         description = "XXXXX";     from = XXXXX;     notification =     {         body = "XXXXX";         e=1;     };     notificationName = "XXXXX";     notificationType = XXXXX;     outbid = XXXXX;     paused = XXXXX;     sound = "XXXXX.wav";     suspended = XXXXX; } 

這裏是我使用產生上述JSON PHP的代碼:

公共函數sendPushNotification($ registration_ids,數組$通知,數組$消息= NULL){

 $url = 'https://fcm.googleapis.com/fcm/send'; 
    $notification['notification']['sound'] = $this->_notificationSoundFile; 
    $fields = array(
     'registration_ids' => array($registration_ids), 
     'notification' => $notification['notification'], 
     'data' => $message['message'] 
    ); 
    $headers = array(
     'Authorization:key=' . $this->_fcmKey, 
     'Content-Type: application/json' 
    ); 


    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields)); 

    $result = curl_exec($ch); 


    if ($result === false) 
     throw new Exception('Curl failed ' . curl_error()); 

    curl_close($ch); 
    if ($result) { 
     return json_decode($result, true)['success'] == true ? true : false; 
    } 
} : 

    enter code here 

Expected Output : 

{ 
    aps =     { 
        alert =         { 
            body = "XXXX"; 
            title = ""; 
        };         
        sound = "XXXX.wav"; 
    }; 
    "gcm.message_id" = "XXXX"; 
    "gcm.notification.appointmentId" = XXXX; 
    "gcm.notification.carCode" = XXXX; 
    "gcm.notification.deal_lost" = XXXX; 
    "gcm.notification.dealerCode" = XXXX; 
    "gcm.notification.notificationName" = "XXXX"; 
    "gcm.notification.notificationType" = XXXX; 
    "gcm.notification.outbid" = XXXX; 
    "gcm.notification.paused" = XXXX; 
    "gcm.notification.suspended" = XXXX; 
} 

Help appreciated. 
+0

是你能夠在你的應用程序在後臺模式 –

+0

是已啓用 –

回答

1

In notification = {alert:「」}

警報應該在那裏。還有一件事backgroundfetch處理方法在你的appdelegate類中實現。

FUNC應用(_應用:UIApplication的,didReceiveRemoteNotification USERINFO:[AnyHashable:任何],fetchCompletionHandler completionHandler:@escaping(UIBackgroundFetchResult) - >空隙){

} 希望它將工作。

1

我已經實現以下應用程序中的委託方法的目標C

德尼亞內什沃Wakchaure

- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage { 
} 
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification 
     withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { 
} 
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response 
     withCompletionHandler:(void (^)())completionHandler { 
} 
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 
} 
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { 
} 
2

您應檢查content_available標籤。這是Apple自動轉換爲aps-> alert的標準FCM有效載荷(用於後臺通知處理)。

{ 
    "notification" : { 
     "title": "XXX", 
     "body" : "xxx", 
     "title": "xxx", 
     "content_available": 1 
    }, 
    "data" : { 
     //contain the payload 
    } 
} 

只是爲了更多參考:FCM guidelines to send notification