【发布时间】:2017-04-28 12:31:11
【问题描述】:
我已经在我的应用程序中实现了 FCM 通知系统,我的问题是它只有在应用程序正在运行或在后台运行时才能正常工作,但如果它被杀死通知不出现,我有这个代码
Manifest.xml
<service android:name="com.Ryuk.listenme.FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
______________________________________________________________
myServer.php
function notify($tokens,$message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = mycode',
'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) {
die('Curl failed: ' . curl_error($ch));
}
curl_close($ch);
return $result;
}
______________________________________________________________
FirebaseMessagingServer.java //which works only in running/background
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService
{
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{
showNotification(remoteMessage.getData().get("message"));
}
private void showNotification(String message)
{
Intent i = new Intent ();
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle("Test FCM")
.setContentText(message)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentIntent(pendingIntent);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0,builder.build());
}
}
我不知道问题出在我的应用程序还是消息格式(在 php 中),我在 google 上尝试了很多解决方案,但一年前它们很现实,但没有奏效
【问题讨论】:
-
这是当应用程序被强制关闭时,还是只是在后台?
-
@Federico De Luca : Check(Log) 你会收到消息 onMessageReceived 与否。
-
我最近遇到了这个问题。有时我会收到通知,但有时我不会。有些人甚至推测 Firebase 白名单的应用程序(如 Whatsapp)即使在通过系统托盘关闭时也会发出通知。stackoverflow.com/a/39505298/4653908。关于这个问题的文档很少。
-
@guido 我很难相信 GCM/FCM 对待应用程序的方式不同。
-
@Shmuel 仅在应用程序被强制关闭时才会发生
标签: android firebase android-service firebase-cloud-messaging