FCM在MainActivity中未收到消息

时间:2020-07-20 19:48:01

标签: java android

我在FCM上遇到了问题,我已经在Google上进行了搜索,但是似乎我检查过的所有网站都帮不了我。

我有2个活动。

首先是MainActivity.java

第二个是SendNotif.java

在第一部手机中打开SendNotif.xml时会收到通知,在第二部手机中也会收到通知,但是当我在第二部手机中重新安装apk并打开它时,默认活动为MainActivity,其中这是当我尝试在第一部手机中SendNotif时,直到我在第二部手机中打开SendNotif时,我才会在第二部手机中不再收到通知。

我想在这里做的是,在第二部手机中,我仍然可以在不打开SendNotif活动的情况下接收通知。

这是我的代码

SENDNOTIF.java

@Override
            public void onClick(View view) {
                sendNotification();
            }
        });
    }
    private void sendNotification(){
        JSONObject mainObj = new JSONObject();
        try {
            mainObj.put("to", "/topics/"+"news");
            JSONObject notificationObj = new JSONObject();
            notificationObj.put("title", "any title");
            notificationObj.put("body", "Test Notification");
            mainObj.put("notification", notificationObj);

            JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, URL, mainObj,
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ejmc.this);
                            alertDialogBuilder.setTitle("Notification Sent!");
                            alertDialogBuilder.setNegativeButton("Okay", null);
                            AlertDialog alertDialog = alertDialogBuilder.create();
                            alertDialog.show();
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ejmc.this);
                    alertDialogBuilder.setTitle("Error!");
                    alertDialogBuilder.setNegativeButton("Retry", null);
                    AlertDialog alertDialog = alertDialogBuilder.create();
                    alertDialog.show();
                }
            }){
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String,String> header = new HashMap<>();
                    header.put("Content-Type", "application/json");
                    header.put("Authorization","key=<key>");
                    return header;
                }
            };

            mRequestQue.add(request);

        } catch (JSONException e){
            e.printStackTrace();
        }
    }

MyFireBaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onNewToken(String s) {
        Log.e("NEW_TOKEN", s);
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Map<String, String> params = remoteMessage.getData();
        JSONObject object = new JSONObject(params);
        Log.e("JSON_OBJECT", object.toString());

        String NOTIFICATION_CHANNEL_ID = "EJMC";

        long pattern[] = {0, 1000, 500, 1000};

        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "EJMC",
                    NotificationManager.IMPORTANCE_HIGH);

            notificationChannel.setDescription("");
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.RED);
            notificationChannel.setVibrationPattern(pattern);
            notificationChannel.enableVibration(true);
            mNotificationManager.createNotificationChannel(notificationChannel);
        }

        // to diaplay notification in DND Mode
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = mNotificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_ID);
            channel.canBypassDnd();
        }

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);

        notificationBuilder.setAutoCancel(true)
                .setColor(ContextCompat.getColor(this, R.color.colorAccent))
                .setContentTitle(getString(R.string.app_name))
                .setContentText(remoteMessage.getNotification().getBody())
                .setDefaults(Notification.DEFAULT_ALL)
                .setWhen(System.currentTimeMillis())
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setAutoCancel(true);


        mNotificationManager.notify(1000, notificationBuilder.build());
    }
}

我该如何处理MainActivity,以便在那里继续接收通知而无需打开SENDNOTIF活动。

0 个答案:

没有答案
相关问题