【问题标题】:when app is running notifcation receiving but Not able to received notification from FCM when app is closed当应用程序正在运行通知接收但在应用程序关闭时无法从 FCM 接收通知
【发布时间】:2026-02-19 10:20:07
【问题描述】:

当应用程序正在运行时接收通知但在应用程序关闭时无法从 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>

【问题讨论】:

标签: php android mysql


【解决方案1】:

请检查一下。

后台受限应用(Android P 或更新版本) 从 2019 年 1 月开始,FCM 将不再向用户设置后台限制的应用程序发送消息(例如通过:设置 -> 应用程序和通知 -> [应用程序名称] -> 电池)。一旦您的应用程序从后台限制中删除,新消息将像以前一样传递到该应用程序。为了防止丢失消息和其他后台限制影响,请确保避免 Android Vitals 努力列出的不良行为。这些行为可能会导致 Android 设备向用户建议您的应用受后台限制。您的应用可以使用 isBackgroundRestricted() 检查它是否受后台限制。

参考:FCM

【讨论】:

  • 有什么办法可以解除这个限制?
  • 设置 -> 应用和通知 -> [appname] -> 电池)
  • 但是当我关闭我的应用程序后再次打开时.m没有收到通知
  • 我应该收到我的旧通知,它是在应用关闭时发送的
最近更新 更多