当应用程序正在运行通知接收但应用程序关闭时无法收到来自 FCM 的通知 看到我已经粘贴了我的 php android 代码
<?php
function send_notification ($tokens, $message)
{
$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
'registration_ids' => $tokens,
'data' => $message
);
$headers = array(
'Authorization:key = AAAAr5QdcA4:APA91brFG8kTIFDP4B3sF2p_VdXjUgY8Z88piRgOl24gczbA51xBXda5wshY3svlNgeQ3v2SqmbzCh1WWTbzR5Jm_FjgdyD6C_7GkphWxjBLKwFFoldcUp82H0O3TXprvuAFgsyo ',
'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;
}
$sql = " Select token From fbtoken ";
$result = mysqli_query($con,$sql);
$tokens = array();
if(mysqli_num_rows($result) > 0 ){
while ($row = mysqli_fetch_assoc($result)) {
$tokens[] = $row["token"];
}
}
mysqli_close($con);
$message = array("message" => "Home Tutor");
$message_status = send_notification($tokens, $message);
echo $message_status;
?>
已收到消息
public void onMessageReceived(RemoteMessage remoteMessage) { sendNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
shownotification(remoteMessage.getData().get("message"));
}
private void shownotification(String message){
Intent intent=new Intent(this,HomePage.class);
intent.addFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder=new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle("FCM Test")
.setContentText(message)
.setSmallIcon(R.drawable.ic_efdapp)
.setContentIntent(pendingIntent);
NotificationManager manager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0,builder.build());
}
<service android:name=".Notification.EFDFirebaseInstanceSerivces">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name=".EFDFireBaseServices"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
最佳答案
请检查一下。
Background Restricted Apps (Android P or newer) Starting Jan 2019, FCM will not deliver messages to apps which were put into background restriction by the user (such as via: Setting -> Apps and Notification -> [appname] -> Battery). Once your app is removed from background restriction, new messages to the app will be delivered as before. In order to prevent lost messages and other background restriction impacts, make sure to avoid bad behaviors listed by the Android vitals effort. These behaviors could lead to the Android device recommending to the user that your app be background restricted. Your app can check if it is background restricted using: isBackgroundRestricted().
引用号:FCM
关于php - 当应用程序正在运行通知接收但应用程序关闭时无法接收来自 FCM 的通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55205097/