当应用程序正在运行时正在接收通知,但在关闭应用程序时无法从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>
答案 0 :(得分:0)
请检查一下。
受背景限制的应用(Android P或更高版本) 从2019年1月开始,FCM将不再向用户设置了后台限制的应用程序传递消息(例如,通过:设置->应用程序和通知-> [应用程序名称]->电池)。将您的应用程序从后台限制中删除后,将像以前一样向该应用程序发送新消息。为了防止消息丢失和其他背景限制影响,请确保避免Android生命周期措施列出的不良行为。这些行为可能导致Android设备向用户推荐您的应用受后台限制。您的应用可以使用isBackgroundRestricted()检查它是否受后台限制。
参考:FCM