简体   繁体   English

应用程序在后台时,Android Firebase推送通知

[英]Android-Firebase Push Notification when app is in background

I am developing Firebase push notification using Server API call in Android application.it works perfectly when application is in foreground but when application is not in foreground i am not able to get the push notification. 我正在使用Android应用程序中的Server API调用开发Firebase推送通知。当应用程序位于前台时,它可以完美地工作,但是当应用程序不在前台时,我无法获得推送通知。

I am sending JSON data in which header contains Server API key and content type and the value contains data which have body as array. 我正在发送JSON数据,其中标头包含服务器API密钥和内容类型,而值包含具有正文作为数组的数据。 Appreciate any help. 感谢任何帮助。

PHP code: 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);?>

I too felt this was happening in the beginning but then discovered that my Android App was indeed waking up. 我也觉得这在一开始就发生了,但是后来发现我的Android应用确实在醒来。 Unfortunately it was not waking up enough to do what I wanted (send a heartbeat REST message to my Server). 不幸的是,它还没有醒来做我想要的事情(向我的服务器发送心跳REST消息)。 So I will answer your question with a question - why did you decide you did not get a push notification ? 因此,我将用一个问题回答您的问题-为什么您确定自己没有收到推送通知? For me, when I logged messages from within the onMessageReceived .... 对我来说,当我从onMessageReceived ...内记录消息时。

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

It proved the App got woken up each time the Fire Cloud Messaging service sent a message, regardless if the App was in foreground or background. 事实证明,每次Fire Cloud Messaging服务发送消息时,应用都会被唤醒,无论该应用处于前台还是后台。

Other mistakes, I made initially I shall share with you: 我最初犯的其他错误将与您分享:

I had to make some changes to my Server side to ensure that the Android App onMessageReceived() method was always called even in background mode. 我必须在服务器端进行一些更改,以确保即使在后台模式下也始终调用Android App的onMessageReceived()方法。 My messages were JSON and HTTP and Upstream. 我的消息是JSON,HTTP和上游。 My server is written in C#. 我的服务器是用C#编写的。

  1. In Firebase there are 2 types of payload downstream messaging. 在Firebase中,有两种类型的有效负载下游消息传递。 (a) Notification and (b) Data. (a)通知和(b)数据。 The type depicts how the App behave when receiving messages depend on whether the app is in background or the foreground. 类型描述了应用程序在接收消息时的行为取决于该应用程序是在后台还是在前台。 Hence I needed (b) Data type payload to meet my requirement. 因此,我需要(b)数据类型有效负载来满足我的要求。 Just add the “data” 只需添加“数据”

  2. I then had to make my messages High Priority rather than normal. 然后,我不得不将消息设置为“高优先级”,而不是正常的。 I did a load of tests with both normal and high. 我进行了正常和较高测试。 Normal did not work once the Android App went into Doze mode. Android应用程序进入打ze模式后,正常无法正常工作。 I found this reference useful. 我发现此参考资料很有用。 https://developer.android.com/training/monitoring-device-state/doze-standby.html#support_for_other_use_cases https://developer.android.com/training/monitoring-device-state/doze-standby.html#support_for_other_use_cases

Look at my code. 看我的代码。 Maybe it will help you: 也许它将帮助您:

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

I use content_available for iOS devices. 我对iOS设备使用content_available That code works for me in Android and iOS when apps are in background. 当应用程序在后台运行时,该代码对我有效。

So you should check your FirebaseMessaging service. 因此,您应该检查FirebaseMessaging服务。 Mine looks like this: 我的看起来像这样:

@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"));
    }
}

暂无
暂无

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

相关问题 Android:当应用未运行或在后台运行时,将Firebase推送通知保存到磁盘 - Android: saving a Firebase push notification to disk when the app is not running or in background Firebase 在 android 中从后台删除应用程序时未收到推送通知 - Firebase push notification not received when app remove from background in android 当应用程序在后台时更新Firebase推送通知 - Updating Firebase push notification when app is in background 当应用程序在Android TV上处于后台时推送通知 - Push notification when app is in the background on Android TV Android-Firebase API - Android-Firebase api 为什么当应用程序处于后台时声音不响(Android应用程序中的Firebase推送通知)? - Why Sound is not ringing when app is in background(Firebase Push Notification in Android app)? Android Firebase:当app在后台时,系统中没有显示华为p8推送通知? - Android Firebase : Huawei p8 push Notification is not shown in system when app is in background? 当应用在后台时,Firebase推送通知不会播放声音 - Firebase Push notification is not playing sound when app is in background 当通过fireBase收到推送通知时,IONIC2在android后台应用程序时增加徽章 - IONIC2 Increment badges while app in background on android when push notification is received via fireBase 当应用程序在android中处于后台时,通知图标不可见(Gcm推送通知)? - Notification Icon not visible (Gcm Push notification) when app is in background in android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM