2016-07-07 169 views
0

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

我正在发送JSON数据,其中头包含Server 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);?> 
+0

为请求提供代码(如果它是一个curl请求或不是),那么你的样本有效载荷,也是你的监听器服务。 –

+2

您最有可能发送通知消息,当您的应用在后台时由操作系统处理。看[这个答案](http://stackoverflow.com/a/37711151/209103)。如果这不是你的问题,你将不得不分享[重现问题的最小代码](http://stackoverflow.com/help/mcve)。 –

+0

@Shreya请修改您的帖子,而不是将您的代码发布为评论。 –

回答

0

我也觉得这是在年初发生,但随后发现,我的Android应用程序确实醒来。不幸的是,它没有足够的唤醒来做我想做的事情(发送心跳REST消息到我的服务器)。所以我会用一个问题回答你的问题 - 你为什么决定你没有收到推送通知?对我来说,当我登录的消息从onMessageReceived内....

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

它证明了应用得到了每一次的消防云消息服务发送消息唤醒,如果不管应用程序在前台或后台。

其他错误,我做了最初我将与大家分享:

我不得不做出一些改变,我的服务器端,以确保即使在背景模式的Android应用程序onMessageReceived()方法总是叫。我的消息是JSON和HTTP以及上游。我的服务器是用C#编写的。

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

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

+0

谢谢你的帮助,但这不适合我,我尝试了所有的事情 – Shreya

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

我用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")); 
    } 
} 
相关问题