Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android接收无提示Firebase通知_Android_Firebase_Push Notification - Fatal编程技术网

Android接收无提示Firebase通知

Android接收无提示Firebase通知,android,firebase,push-notification,Android,Firebase,Push Notification,您好,我有android应用程序,我使用了Firebase通知,它工作良好,现在我需要在没有警报或任何东西的情况下接收静默推送,,,我尝试了一些想法,当应用程序运行时,它工作,但当应用程序在后台或终止时,它不工作!如果有人想独家解决这个问题,请告诉我:)这是我的代码 public class MyFirebaseMessagingService extends FirebaseMessagingService { Boolean isSilent; String Silent

您好,我有android应用程序,我使用了Firebase通知,它工作良好,现在我需要在没有警报或任何东西的情况下接收静默推送,,,我尝试了一些想法,当应用程序运行时,它工作,但当应用程序在后台或终止时,它不工作!如果有人想独家解决这个问题,请告诉我:)这是我的代码

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    Boolean isSilent;
    String Silent = "";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        try {
            Bundle bundle = new Bundle();
            for (Map.Entry<String, String> entry : remoteMessage.getData().entrySet()) {
                bundle.putString(entry.getKey(), entry.getValue());
                Log.d(entry.getKey(), entry.getValue());
            }
//            remoteMessage.getData().get("screen_id")
            if (remoteMessage.getData().size() > 0) {
                sendNotificationData(bundle.getString("data_title"), bundle.getString("data_body"), bundle.getString("screen_id"));

            } else if (remoteMessage.getNotification() != null) {
                sendNotification(remoteMessage.getNotification(), bundle.getString("screen_id"));
            }


        } catch (Exception ex) {
            ex.printStackTrace();

        }
    }

    private void sendNotification(RemoteMessage.Notification notificationR, String screenId) {
        NotificationManager nManager = (NotificationManager) this.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intentNotification = new Intent(this, MainActivity.class);
        intentNotification.putExtra("screen_id", screenId);
        Log.v("sendNotification ", " >>>>>####>>>>>>>>  " + screenId);
        // finish previous activity
        if (!Silent.equals("yes")) {
            intentNotification.addFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intentNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 50, intentNotification, PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this.getApplicationContext())
                    .setContentTitle(notificationR.getTitle())
                    .setContentText(notificationR.getBody())
                    .setSmallIcon(getNotificationIcon())
                    .setLargeIcon(icon(getApplicationContext()))
                    .setLights(Color.LTGRAY, 1000, 1000)
                    .setAutoCancel(true)
                    .setTicker(notificationR.getTitle())
                    .setContentIntent(pendingIntent)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));


            Notification notification = notificationBuilder.build();
            //  '|' Binary OR Operator copies a bit if it exists in either operand. to ensure no conflict on the flags
            notification.flags = notification.flags | Notification.FLAG_SHOW_LIGHTS;

            nManager.notify((int) SystemClock.currentThreadTimeMillis(), notification);

        }
    }


    private void sendNotificationData(String dataTitle, String dataBody, String screenId) {
        NotificationManager nManager = (NotificationManager) this.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intentNotification = new Intent(this, MainActivity.class);
        intentNotification.putExtra("screen_id", screenId);


        if (!Silent.equals("yes")) {
            intentNotification.addFlags(android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intentNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 50, intentNotification, PendingIntent.FLAG_UPDATE_CURRENT);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this.getApplicationContext())
                    .setContentTitle(dataTitle)
                    .setContentText(dataBody)
                    .setSmallIcon(getNotificationIcon())
                    .setLargeIcon(icon(getApplicationContext()))
                    .setLights(Color.LTGRAY, 1000, 1000)
                    .setAutoCancel(true)
                    .setTicker(dataTitle)
                    .setContentIntent(pendingIntent)
                    .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

            Notification notification = notificationBuilder.build();
            //  '|' Binary OR Operator copies a bit if it exists in either operand. to ensure no conflict on the flags
            notification.flags = notification.flags | Notification.FLAG_SHOW_LIGHTS;

            nManager.notify((int) SystemClock.currentThreadTimeMillis(), notification);
        }
       @Override
        public boolean zzE(Intent intent) {
            isSilent = intent.hasExtra("silent");
            if (isSilent) {
                Silent = "yes";
            }
            return super.zzE(intent);
        }
公共类MyFirebaseMessagingService扩展了FirebaseMessagingService{
布尔互不相关;
String Silent=“”;
@凌驾
收到消息时公共无效(RemoteMessage RemoteMessage){
试一试{
Bundle=新Bundle();
对于(Map.Entry:remoteMessage.getData().entrySet()){
bundle.putString(entry.getKey(),entry.getValue());
Log.d(entry.getKey(),entry.getValue());
}
//remoteMessage.getData().get(“屏幕id”)
如果(remoteMessage.getData().size()>0){
sendNotificationData(bundle.getString(“数据标题”)、bundle.getString(“数据正文”)、bundle.getString(“屏幕id”);
}else if(remoteMessage.getNotification()!=null){
sendNotification(remoteMessage.getNotification(),bundle.getString(“屏幕id”);
}
}捕获(例外情况除外){
例如printStackTrace();
}
}
私有void sendNotification(RemoteMessage.Notification notificationR,字符串screenId){
NotificationManager nManager=(NotificationManager)this.getApplicationContext().getSystemService(Context.NOTIFICATION_服务);
意向意向通知=新意向(此,MainActivity.class);
intentNotification.putExtra(“屏幕id”,屏幕id);
Log.v(“sendNotification”、“>>>>>####>>>”+屏幕ID);
//完成之前的活动
如果(!Silent.equals(“yes”)){
intentNotification.addFlags(android.content.Intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
intentNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingEvent PendingEvent=PendingEvent.getActivity(this,50,intentNotification,PendingEvent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder=新建NotificationCompat.Builder(this.getApplicationContext())
.setContentTitle(notificationR.getTitle())
.setContentText(notificationR.getBody())
.setSmallIcon(getNotificationIcon())
.setLargeIcon(图标(getApplicationContext()))
.setLights(Color.LTGRAY,10001000)
.setAutoCancel(真)
.setTicker(notificationR.getTitle())
.setContentIntent(挂起内容)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知));
Notification Notification=notificationBuilder.build();
//“|”二进制或运算符复制位(如果位存在于任一操作数中)。以确保标志不冲突
notification.flags=notification.flags | notification.FLAG_SHOW_LIGHTS;
nManager.notify((int)SystemClock.currentThreadTimeMillis(),通知);
}
}
私有void sendNotificationData(字符串dataTitle、字符串dataBody、字符串screenId){
NotificationManager nManager=(NotificationManager)this.getApplicationContext().getSystemService(Context.NOTIFICATION_服务);
意向意向通知=新意向(此,MainActivity.class);
intentNotification.putExtra(“屏幕id”,屏幕id);
如果(!Silent.equals(“yes”)){
intentNotification.addFlags(android.content.Intent.FLAG\u ACTIVITY\u CLEAR\u TOP);
intentNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingEvent PendingEvent=PendingEvent.getActivity(this,50,intentNotification,PendingEvent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder=新建NotificationCompat.Builder(this.getApplicationContext())
.setContentTitle(数据标题)
.setContentText(数据体)
.setSmallIcon(getNotificationIcon())
.setLargeIcon(图标(getApplicationContext()))
.setLights(Color.LTGRAY,10001000)
.setAutoCancel(真)
.setTicker(数据标题)
.setContentIntent(挂起内容)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_通知));
Notification Notification=notificationBuilder.build();
//“|”二进制或运算符复制位(如果位存在于任一操作数中)。以确保标志不冲突
notification.flags=notification.flags | notification.FLAG_SHOW_LIGHTS;
nManager.notify((int)SystemClock.currentThreadTimeMillis(),通知);
}
@凌驾
公共布尔zzE(意图){
isSilent=intent.hasExtra(“沉默”);
如果(Isilent){
Silent=“是”;
}
返回super.zzE(intent);
}

当应用程序正在运行时,此代码正在后台工作,但已停止工作!!

当您的应用程序在后台时,会在“系统托盘”上收到数据库通知消息,这就是为什么未调用FirebaseMessagingService.onMessageReceived的原因

当你的应用程序在后台时,Android会将通知消息定向到系统托盘。默认情况下,用户点击通知会打开应用程序启动器

尝试发送将调用onMessageReceived的数据有效负载。您的代码将 工作 对于数据消息。。。。 从FirebaseInstancedService获取firebase令牌并将其发送到app server。 谷歌有很好的文档发送它

您也可以使用php发送它

然后,我收到了一封邮件

if (remoteMessage.getData().size() > 0) {
        String title = remoteMessage.getData().get("title");
        String body = remoteMessage.getData().get("body");
        String screen = remoteMessage.getData().get("screen");

        sendNotification(title, body, screen );}
用于服务器端php实现
    <?php  
$path_to_fcm = "https://fcm.googleapis.com/fcm/send";
$headers = array(
    'Authorization:key='YOUR_SERVER_KEY,
    'Content-Type:application/json'
);

$reg_id_array = array
(
    'token1',
    'token2'
)

$mesg = array
(   
    'title'=>$_POST['title'], 
    'body'=> $_POST['message'], 
    'url'=>$_POST['Screen'],

);

$fields = array("registration_ids"=>$reg_id_array, 'data'=>$mesg);


$payload = json_encode($fields);
$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);

$Qresult = curl_exec($curl_session);
$httpcode = curl_getinfo($curl_session , CURLINFO_HTTP_CODE);
curl_close($curl_session);
if ($httpcode==200) {
    echo "Success";
}
?>