应用在后台时的Android-Firebase推送通知

时间:2016-07-07 11:17:51

标签: android firebase firebase-cloud-messaging

我正在使用Android应用程序中的服务器API调用开发Firebase推送通知。当应用程序在前台但当应用程序不在前台时,我无法获得推送通知。

我发送的JSON数据包含服务器API密钥和内容类型,并且该值包含将body作为数组的数据。感谢任何帮助。

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);?>

2 个答案:

答案 0 :(得分:0)

我也觉得这在一开始就发生了,但后来发现我的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服务发送消息时,应用程序都会被唤醒,无论应用程序是在前台还是后台。

其他错误,我最初会与你分享:

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

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

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

答案 1 :(得分:0)

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

$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);

我对iOS设备使用 content_available 。当应用程序处于后台时,该代码适用于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"));
    }
}
相关问题