0

when app is running notifcation receiving but Not able to received notification from FCM when app is closed see i have paste my php android code

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

messagereceived

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>
il_raffa
  • 5,090
  • 129
  • 31
  • 36
nitin_efd
  • 1
  • 2

1 Answers1

0

Please check this out.

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().

Ref:FCM

Prafulla Malviya
  • 375
  • 3
  • 17