[英]FCM is not working when the app is killed
我正在使用 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());
}
}
這是通知發送方 class 代碼
公共 class 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();
}
}
許多中國制造商為了節省電池而關閉了該應用程序的后台服務。 通知服務的運行是接收推送通知的主要方式。 但是,如果您可以在“設置”中將您的應用從電池優化中刪除,那么當您的應用處於后台或您已終止該應用時,您將收到推送通知。 這可能看起來很傻,但是當我遇到同樣的問題時,我從電池優化中刪除了我的應用程序並且它起作用了!
像這樣:
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");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.