【问题标题】:Android-Firebase Push Notification when app is in background应用程序在后台时的 Android-Firebase 推送通知
【发布时间】:2016-08-30 22:42:24
【问题描述】:

我正在使用 Android 应用程序中的服务器 API 调用开发 Firebase 推送通知。当应用程序处于前台时,它可以完美运行,但当应用程序不在前台时,我无法获得推送通知。

我正在发送 JSON 数据,其中标头包含服务器 API 密钥和内容类型,值包含具有正文为数组的数据。感谢任何帮助。

PHP 代码:

 $url = 'https://fcm.googleapis.com/fcm/send';
 $fields =array('registration_ids' => $tokens,'data' => $message); 
 $headers = array('Authorization:key = value','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);?>

【问题讨论】:

  • 提供您的请求代码(如果是 curl 请求或其他),然后是您的示例负载,以及您的侦听器服务。
  • 您很可能正在发送一条通知消息,当您的应用在后台时,该消息由操作系统处理。见this answer。如果这不是您的问题,您必须分享minimal code that reproduces the problem
  • @Shreya 请编辑您的帖子,而不是将您的代码作为评论发布。
  • @AL done 我已经编辑了问题

标签: android firebase firebase-cloud-messaging


【解决方案1】:

看看我的代码。也许它会帮助你:

$token = getTokenDevice($dn);

  $path_to_firebase_cm = 'https://fcm.googleapis.com/fcm/send';

  $fields = array(
      'to' => $token,
      'priority' => 'normal',
      'content_available' => true,
      'data' => array('type' => $type, 'title' => $title, 'body' => $message)
  );

  $headers = array(
      'Authorization:key='.$server_key,
      'Content-Type:application/json'
  );
  $ch = curl_init();

  curl_setopt($ch, CURLOPT_URL, $path_to_firebase_cm);
  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_IPRESOLVE, CURL_IPRESOLVE_V4 );
  curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));

  $result = curl_exec($ch);

  curl_close($ch);

我将 content_available 用于 iOS 设备。当应用在后台时,该代码适用于我在 Android 和 iOS 中。

因此,您应该检查您的 FirebaseMessaging 服务。我的看起来像这样:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Auth auth = new Auth(getApplicationContext());

    Map<String, String> data = remoteMessage.getData();

    if (auth.isNotificationEnabled(data.get("type"))) {
        Log.e(TAG, "data: " + remoteMessage.getData());
        sendNotification(data.get("title"), data.get("body"));
    }
}

【讨论】:

    【解决方案2】:

    一开始我也觉得会发生这种情况,但后来发现我的 Android 应用确实在醒来。不幸的是,它没有醒来,无法做我想做的事(向我的服务器发送心跳 REST 消息)。所以我会用一个问题来回答你的问题——你为什么决定没有收到推送通知?对我来说,当我从 onMessageReceived 中记录消息时 ....

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {  
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + remoteMessage.getData());
     }
    

    证明每次 Fire Cloud Messaging 服务发送消息时,App 都会被唤醒,无论 App 是在前台还是后台。

    我最初犯的其他错误我将与您分享:

    我必须对我的服务器端进行一些更改,以确保即使在后台模式下也始终调用 Android 应用程序的 onMessageReceived() 方法。我的消息是 JSON 和 HTTP 以及上游。我的服务器是用 C# 编写的。

    1. 在 Firebase 中有 2 种类型的有效负载下游消息传递。 (a) 通知和 (b) 数据。该类型描述了应用程序在接收消息时的行为取决于应用程序是在后台还是前台。因此,我需要 (b) 数据类型有效负载来满足我的要求。只需添加“数据”

    2. 然后我不得不将我的消息设置为高优先级而不是正常。我做了很多正常和高的测试。 Android 应用程序进入打盹模式后,正常无法正常工作。我发现这个参考很有用。 https://developer.android.com/training/monitoring-device-state/doze-standby.html#support_for_other_use_cases

    【讨论】:

    • 感谢您的帮助,但这对我不起作用,我尝试了所有方法
    猜你喜欢
    • 1970-01-01
    • 2022-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多