Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.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 应用程序上的FCM已关闭_Android_Firebase_Android Service_Firebase Cloud Messaging - Fatal编程技术网

Android 应用程序上的FCM已关闭

Android 应用程序上的FCM已关闭,android,firebase,android-service,firebase-cloud-messaging,Android,Firebase,Android Service,Firebase Cloud Messaging,我已经在我的应用程序中实现了FCM通知系统,我的问题是,只有当应用程序正在运行或在后台运行时,它才能正常工作,但如果它被杀死,则不会出现通知,我有以下代码 Manifest.xml <service android:name="com.Ryuk.listenme.FirebaseMessagingService"> <intent-filter> <action android:name="com.google.fireba

我已经在我的应用程序中实现了FCM通知系统,我的问题是,只有当应用程序正在运行或在后台运行时,它才能正常工作,但如果它被杀死,则不会出现通知,我有以下代码

Manifest.xml
 <service android:name="com.Ryuk.listenme.FirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
</service>
______________________________________________________________

myServer.php

function notify($tokens,$message)
{

$url = 'https://fcm.googleapis.com/fcm/send';
$fields = array(
              'registration_ids' => $tokens,
              'data' => $message         
              );
$headers = array(
                'Authorization:key = mycode',
                '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;
}
______________________________________________________________

FirebaseMessagingServer.java //which works only in running/background

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService
{
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
{

    showNotification(remoteMessage.getData().get("message"));
}

private void showNotification(String message)
{
    Intent i = new Intent ();
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_ONE_SHOT);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
            .setAutoCancel(true)
            .setContentTitle("Test FCM")
            .setContentText(message)
            .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)

            .setContentIntent(pendingIntent);
    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0,builder.build());

}
}
Manifest.xml
______________________________________________________________
myServer.php
函数通知($tokens,$message)
{
$url='1https://fcm.googleapis.com/fcm/send';
$fields=数组(
“注册ID”=>$tokens,
“数据”=>$message
);
$headers=数组(
'授权:key=mycode',
'内容类型:应用程序/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\u exec($ch);
如果($result==FALSE){
模具('Curl失败:'。Curl_错误($ch));
}
卷曲关闭($ch);
返回$result;
}
______________________________________________________________
FirebaseMessagingServer.java//仅在运行/后台工作
公共类FirebaseMessagingService扩展了com.google.firebase.messagingService.FirebaseMessagingService
{
@凌驾
收到消息时公共无效(RemoteMessage RemoteMessage)
{
showNotification(remoteMessage.getData().get(“消息”));
}
私有void showNotification(字符串消息)
{
意图i=新意图();
i、 添加标志(意图、标志、活动、清除、顶部);
PendingEvent PendingEvent=PendingEvent.getActivity(这个,0,i,PendingEvent.FLAG_一个镜头);
NotificationCompat.Builder=新建NotificationCompat.Builder(此)
.setAutoCancel(真)
.setContentTitle(“测试FCM”)
.setContentText(消息)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentIntent(挂起内容);
NotificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION\u服务);
manager.notify(0,builder.build());
}
}
我不知道问题是在我的应用程序中还是在我的消息格式(php)中,我在谷歌上尝试了这么多解决方案,但一年前它们是现实的,根据文档,它们不起作用

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

这包括同时包含通知和数据的消息 有效载荷。在这些情况下,通知会发送到设备的 系统托盘,数据有效负载在 您的启动器活动的意图


当您处于
后台时
,FCM将根据通知有效负载中的信息在系统托盘中显示通知。标题、消息和图标从通知负载中获取。有关详细信息,请参阅。

一旦应用程序被强制关闭,您将不会收到背景意图。这 包括GCM/FCM消息

“从Android 3.1开始,系统的软件包管理器跟踪处于停止状态的应用程序,并提供一种从后台进程和其他应用程序控制其启动的方法。”

“请注意,系统在所有广播意图中添加标志_EXCLUDE_STOPPED_包。这样做是为了防止来自后台服务的广播无意或不必要地启动已停止应用程序的组件。”


有趣的是,三星的设备经常使用“优化应用程序”功能来杀死后台应用程序。这可能会影响你的应用程序。查看此处了解更多信息-

我已经解决了这个问题,这是我手机中的一个设置,它可以阻止通知

这是当应用程序被强制关闭时,还是仅仅在后台?@Federico De Luca:检查(日志)你是否会收到MessageReceived上的消息。我最近一直在努力解决这个问题。有时我会收到通知,但有时我不会。有些人甚至猜测Firebase白名单的应用程序,如Whatsapp,即使通过系统托盘关闭,也会发出通知。。关于这个问题的文档很少。@guido我发现很难相信GCM/FCM会以不同的方式对待应用程序。@Shmuel只有当应用程序强制关闭时才会发生这种情况,但我如何才能做到这一点?我必须编辑我的php代码?对不起,也许我是noob,但我不明白我应该把有效载荷代码放在哪里…这没有帮助,它没有解决问题。这是明确指出,问题不是当应用程序在后台运行,而是当它完全关闭(根本没有运行)不幸的是,它只工作了一天,我暂时投降,希望在一些fcm更新=(