【问题标题】:FCM push notification not working when app is closed Android当应用程序关闭Android时,FCM推送通知不起作用
【发布时间】:2016-11-19 06:40:45
【问题描述】:

好的,所以我到处寻找,但找不到答案。 我已经在我的 Android 应用程序中实现了推送通知,并且在应用程序处于活动状态(前台或后台)时一切正常,但是如果我关闭应用程序,我将停止接收通知。 这是我发送通知的 php 代码。

public static function sendNotification($token){
    $url = 'https://fcm.googleapis.com/fcm/send';
    $fields = array(
        'notification' => array("title" => "Test","body" => "Test Message","icon" => "default","sound" => "default"),
        "data" => "test message",
        'to' => $token
    );
    $headers = array(
        'Authorization:key = AIzaSyAKHM3MoMACjmeVK46TDg8-rTj1KoVjzWs',
        '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_VERIFYHOST,0);
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);                           
    curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields));
    $result = curl_exec($ch);
    if($result === FALSE) {
        throw new errorSendingNotification();
    }
    curl_close($ch);
    // Result returns this
    // {"multicast_id":8978533958735781479,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1468627796530714%7c0e4bee7c0e4bee"}]}
    return $result;
}

【问题讨论】:

  • 您的应用中有通知服务吗?
  • 我也面临 FCM 的问题,请问在收到您在评论行中提到的回复后,请问如何处理该回复
  • 我没有为我的应用创建任何后端服务器,只是在我的 MainActivity 中调用 api

标签: java php android curl push-notification


【解决方案1】:

我遇到了同样的问题。这对我有帮助。

请从有效负载中删除“通知”键并仅提供“数据”键。

应用程序仅在应用程序处于前台时处理通知消息,但即使应用程序处于后台或关闭,它也会处理数据消息。

如果应用程序在前台,onMessageReceived 处理 datanotification 消息。如果应用程序已关闭或在后台,则只有 data 消息会传递到 onMessageReceived 。 notification 消息不会传递到 onMessageReceived 。所以你不能自定义你的消息。

最好从有效负载中删除 notification 键。

【讨论】:

  • 在这种情况下,您可以覆盖 FirebaseMessagingService 中的 handleIntent。当应用程序关闭时,此方法将触发
【解决方案2】:

AmAnDroid 提供了一个解决方案。

第二个解决方案是: 不幸的是,这是 SDK 9.0.0-9.6.1 中 Firebase 通知的限制。当应用程序在后台时,启动器图标会从清单(带有必要的 Android 着色)中用于从控制台发送的消息。

但是,使用 SDK 9.8.0,您可以覆盖默认值!在您的 AndroidManifest.xml 中,您可以设置以下字段来自定义图标和颜色:

在清单中添加以下代码:

 <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/com_facebook_button_send_icon_blue" />

    <meta-data android:name="com.google.firebase.messaging.default_notification_color"
        android:resource="@color/bt_blue_pressed" />

身体/有效载荷:

{ "notification": 
 {
   "title": "Your Title",
   "text": "Your Text",
   "click_action": "MAIN_ACTIVITY" // should match to your intent filter
 },
 "data": 
 {
   "keyname": "any value " //you can get this data as extras in your activity and this data is optional
 },
 "to" : "to_id(firebase refreshedToken)"
} 

在您要调用的活动中添加以下代码(意图过滤器):

<activity
        android:name="MainActivity"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="MAIN_ACTIVITY" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
 </activity>

确保您的 Body/Payload 的 "click_action": "MAIN_ACTIVITY" 和 intent-filter 的 android:name="MAIN_ACTIVITY" 应该匹配。

【讨论】:

  • 您的问题到底是什么? @AjayPandya
  • 让我尝试另一种方法,我现在在 ravitamada 的一个博客上找到了,否则我会在这里详细告诉你,或者如果需要,我会向你展示代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-11
  • 1970-01-01
  • 2019-01-11
  • 2022-01-13
  • 2021-03-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多