繁体   English   中英

有时未收到推送通知 firebase

[英]Push notification firebase sometime not received

我有有时没有收到通知的问题。 为了接收通知,我需要重新启动我的应用程序,然后执行notification.php并出现通知。但是在 30 分钟/15 分钟后,当我再次尝试运行notification.php ,没有收到任何通知(我需要再次运行我的应用程序)。 我希望你们能帮我解决这个问题。先谢谢你。

通知.php

<?php

require "../../init.php";
include("../../function_lib.php");

$id  = $_GET['id'];

$title = "Try App";
$message = "news..";
$path_to_fcm = "https://fcm.googleapis.com/fcm/send"; //send push notification through this firebase url
$server_key = "####";

$topic = "/topics/reminder";
$type = "reminder";



//create http request to firease server
//request need 2 section : 1.header section 2. payload section
//add push notification in payload section

//header section for http request : that content authorization key and content_type of this application

//below are the header section of the http request
$headers = array(
            'Authorization:key='.$server_key,
            'Content-Type:application/json'
        );



/*

$field = array('to'=>$topic,'notification'=>array("title"=>"add title","text"=>"add text","click_action"=>"OPEN_ACTIVITY_1"),
                'data'=>array('title'=>$title,'body'=>$message,'type'=>$type,'id'=>$id),'priority'=>"high");
*/
//echo "\n".$key;
// to = refer to fcm token, notification refer to message that wull be use in push notification.
///below are the payload section.
$field = array('to'=>$topic,
                'data'=>array('title'=>$title,'body'=>$message,'type'=>$type,'id'=>$id));

//perform json encode
$payload = json_encode($field);


//pass $payload (content json encode) variable to firebase server

//below  gonna start url section.gonna use firebase url http to send json to firebase server.
$curl_session = curl_init();
curl_setopt($curl_session,CURLOPT_URL, $path_to_fcm);
curl_setopt($curl_session,CURLOPT_POST,true);
curl_setopt($curl_session,CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl_session,CURLOPT_RETURNTRANSFER,true); 
curl_setopt($curl_session,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl_session,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4);
curl_setopt($curl_session,CURLOPT_POSTFIELDS, $payload);

//execute curl
$result = curl_exec($curl_session);

//close curl
curl_close($curl_session);

//close  mysqli
//mysql_close($conn);


$path = "../dashboard.php?act=home";
echo ('<script>location.href="'.$path.'"</script>');

?>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.mas.khoi.infoevent">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/icon_logo_only"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">


        <service android:name=".firebase.FcmInstanceIdService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>
        <service android:name=".firebase.FcmMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

        <activity android:name=".ScreenActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".fragment.MainActivity">

        </activity>
        <activity android:name=".EventDetailsActivity">
        </activity>

    </application>

</manifest>

FcmInstanceIdService.java

public class FcmInstanceIdService extends FirebaseInstanceIdService {

    @Override
    public void onTokenRefresh() {

        //http://stackoverflow.com/questions/38340526/firebase-fcm-token-when-to-send-to-server
        String recent_token = FirebaseInstanceId.getInstance().getToken();

        //add token to sharepreference
        SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(Config.SETTING, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        Log.i("generate token",recent_token);
        editor.putString(Config.SETTING_FCM_TOKEN,recent_token);
        editor.commit();
    }
}

FcmMessagingService.java

public class FcmMessagingService extends FirebaseMessagingService {


    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        //if want to use function below need to used change data to notification at $field at server side.
        //String message = remoteMessage.getNotification().getBody();
        //String title = remoteMessage.getNotification().getTitle();

        if(remoteMessage.getData().size()>0){
            Log.i("Notification Sucess",""+remoteMessage.getData());
            startNotification(remoteMessage);
        }else{
            Log.i("Notification Failed",""+remoteMessage.getData());
        }

    }


    public void startNotification(RemoteMessage remoteMessage){
        String title = remoteMessage.getData().get("title");
        String messsge = remoteMessage.getData().get("body");
        String type = remoteMessage.getData().get("type");
        String id = remoteMessage.getData().get("id");


        Log.i("Notification","active");
        //Log.i("Notificaition","type:"+type);
        //Log.i("Notificaition","id:"+id);

        //check if notification eihter is reminder or new quote/event
        if (!type.equals("reminder") && id.equals("none")) {

            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
            notificationBuilder.setContentTitle(title);
            notificationBuilder.setContentText(messsge);
            notificationBuilder.setSmallIcon(R.drawable.icon_no_bg);
            notificationBuilder.setAutoCancel(true);
            notificationBuilder.setContentIntent(pendingIntent);

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, notificationBuilder.build());
            //super.onMessageReceived(remoteMessage);
        }else

        if(type.equals("reminder") && ! id.equals("none")) {

            Intent intent = new Intent(this, EventDetailsActivity.class);
            intent.putExtra("id",id);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);
            notificationBuilder.setContentTitle(title);
            notificationBuilder.setContentText(messsge);
            notificationBuilder.setSmallIcon(R.drawable.icon_logo_only);
            notificationBuilder.setAutoCancel(true);
            notificationBuilder.setContentIntent(pendingIntent);

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, notificationBuilder.build());
        }

    }
}

请查看有关何时触发 onMessageReceived() 方法的参考

onMessageReceived() 只有当应用程序在前台接收通知消息时才会触发,但对于数据消息,它总是会得到处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM