【问题标题】:FCM is not working when the app is killed当应用程序被杀死时,FCM 不工作
【发布时间】:2020-07-17 16:47:35
【问题描述】:

我正在使用 fcm 在我的应用程序中发送通知,但是当应用程序关闭并从最近的应用程序中清除时,通知不会到来。我会使用 FCM api 向特定主题发送通知。 注意:- 当应用程序在后台时,通知工作正常

这是我的 FirebaseMessagingService 代码

@RequiresApi(api = Build.VERSION_CODES.O)

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {


    String title = remoteMessage.getNotification().getTitle();
    String body = remoteMessage.getNotification().getBody();

    Map<String, String> extraData = remoteMessage.getData();

 Log.d("DATA",remoteMessage.getData().toString());

    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this, "TAC")
                    .setContentTitle(title)
                    .setContentText(body)
                    .setSmallIcon(R.drawable.ic_launcher_background);





    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);


    int id =  (int) System.currentTimeMillis();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
        NotificationChannel channel = new NotificationChannel("TAC","demo",NotificationManager.IMPORTANCE_HIGH);
        notificationManager.createNotificationChannel(channel);
    }
    notificationManager.notify(id,notificationBuilder.build());

}

}

这是通知发送器类代码

公共类 NotificationSender {

public  static RequestQueue mRequest;
public static String URL="https://fcm.googleapis.com/fcm/send";

public static void sendnotification(Context context,String sendto, String title, JSONObject extra){
    mRequest = Volley.newRequestQueue(context);
    JSONObject mainbody = new JSONObject();
    try {
        mainbody.put("to","/topics/"+sendto);
          mainbody.put("priority","high");
        JSONObject notification = new JSONObject();
        notification.put("title","New Notification");
        notification.put("body",title);
        mainbody.put("notification",notification);
          //  mainbody.put("data",extra);
            JSONObject data = new JSONObject();
            data.put("key",title);
            mainbody.put("data",data);


        JsonObjectRequest request=new JsonObjectRequest(Request.Method.POST, URL, mainbody, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String,String> header = new HashMap<>();
                header.put("content-type","application/json");
                header.put("authorization","key=AIzaSyAfZqD0MW39WIGRBDFG0si3-HszFA");
               // AIzaSyAfZqD0MW39WIu9pWraYA0AG0si3-HszFA
                return header;
            }
        };
        mRequest.add(request);
    } catch (JSONException e) {
        e.printStackTrace();
    }






}

}

这是我关闭电池优化的代码

 if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        final Intent intent = new Intent();
        final String packageName = getPackageName();
        PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
        if (!pm.isIgnoringBatteryOptimizations(packageName)) {
            AlertDialog ad= new AlertDialog.Builder(this).setTitle("IMPORTANT").setMessage("For The Proper Working Of The App,Please Disable Battery Optimization For This App").setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
                    intent.setData(Uri.parse("package:" + packageName));
                    startActivity(intent);

                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finishAffinity();
                    System.exit(0);
                }
            }).create();
            ad.show();
        }
    }

【问题讨论】:

  • 你可以看起来和question一样。我在那里解释过。
  • 先生那么它也不起作用
  • 您的主体包含通知标签。当应用程序被杀死时,它不会以这种方式工作
  • 那我应该改变什么
  • 您应该删除通知。你只需要发送数据

标签: android firebase firebase-cloud-messaging firebase-notifications


【解决方案1】:

许多中国制造商为了省电而终止了应用程序的后台服务。通知服务的运行是接收推送通知的主要内容。 但是,如果您可以在“设置”中将您的应用从电池优化中删除,那么当您的应用处于后台或您已终止该应用时,您将收到推送通知。 这可能看起来很傻,但是当我遇到同样的问题时,我从电池优化中删除了我的应用程序并且它起作用了!

【讨论】:

  • 先生,我已经关闭了电池优化。我也发布了相同的代码
  • 您是否在“设置”中查看代码是否已将您的应用从电池优化中移除?
  • 当我点击关闭时应用程序要求关闭电池优化的代码存在问题,但它也没有关闭我的电池优化。但在某些手机中它在关闭后工作手动我收到通知你能分享代码来通过程序来做吗
  • 我认为这无关。即使我打开了优化,VOIP 应用程序仍然可以正常工作。
【解决方案2】:

像这样:

mainbody.put("to","/topics/"+sendto);
mainbody.put("priority","high");
JSONObject data = new JSONObject();
data("title","New Notification");
data("body",title);
mainbody.put("data",data);

和

Map<String, String> extraData = remoteMessage.getData();

String title = extraData.get("title");
String body = extraData.get.get("body");

【讨论】:

  • 应用被杀时它不工作我使用的是一加 7 并且电池优化也关闭了
  • 先生,我只是将您的代码复制粘贴到我的应用程序中,但问题存在
  • 对不起,我要求您再次检查您的代码。你可能遗漏了什么。
  • 你是对的。没有问题。你能检查一下,当应用程序前台时是否收到通知
  • 看不懂,这个方法我用了很多年了。有问题。可以是“话题”吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多