简体   繁体   English

我没有在iOS上的后台收到FCM推送通知

[英]I am not receiving fcm push notifications in background on IOS

This is my json that I am sending but I am only receiving notifications in foreground in IOS but not in background. 这是我要发送的json,但我只在IOS的前台接收通知,而在后台则没有。

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; }

Here is the php code that I am using to generate the above json : 这是我用来生成上述json的php代码:

public function sendPushNotification($registration_ids, array $notification, array $message= null) { 公共功能sendPushNotification($ registration_ids,数组$ notification,数组$ message = 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.

In notification = { alert : ""} 通知中= {警报:“”}

Alert should be there. 警报应该在那里。 And one more thing backgroundfetch handler method implement in you appdelegate class. 在appdelegate类中还实现了backgroundfetch处理程序方法的另一件事。

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { func application(_ application:UIApplication,didReceiveRemoteNotification userInfo:[AnyHashable:Any],fetchCompletionHandler completeHandler:@escaping(UIBackgroundFetchResult)-> Void){

} Hope it will work. }希望它能起作用。

You should check for the content_available tag. 您应该检查content_available标签。 This is a standard FCM payload that Apple automatically converts to aps->alert (For Background notification handling). 这是标准的FCM有效内容,Apple会自动将其转换为aps-> alert(用于后台通知处理)。

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

Just for more reference : FCM guidelines to send notification 仅供参考: FCM准则发送通知

I have implemented following methods in app Delegate in objective C 我已经在目标C中的app Delegate中实现了以下方法

Dnyaneshwar Wakchaure Dnyaneshwar 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 {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM