简体   繁体   English

如果应用程序在前台,通知不会显示在通知抽屉中

[英]Notification is not displayed in notification drawer if app is in foreground

I am sending a notification into my app using Firebase cloud messaging and it's working fine.我正在使用 Firebase 云消息传递向我的应用程序发送通知,并且工作正常。 However, when I send a notification and if my app is in the foreground, the notification will not show in the notification tray.但是,当我发送通知并且我的应用程序在前台时,通知将不会显示在通知托盘中。 The notification only shows in the notification tray if my app is in the background.如果我的应用程序在后台,通知仅显示在通知托盘中。

Below is my code:下面是我的代码:

public class Notifications extends FirebaseMessagingService {

    @ServerTimestamp
    Date time;
    private FirebaseFirestore mFirestore;
    private boolean isNotificationMatching = false;

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        super.onMessageReceived(remoteMessage);

        mFirestore = FirebaseFirestore.getInstance();

        if (remoteMessage.getData() != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
                sendNotificationAPI26(remoteMessage);

            else
                sendNotification(remoteMessage);
        }
    }

    private void sendNotification(RemoteMessage remoteMessage) {

        isNotificationMatching = false;
        Intent intent = new Intent(this, Dashboard.class);
        intent.putExtra("notificationFragment", "showNotifications");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);


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

        String title = data.get("title");

        mFirestore.collection("notifications").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        if (document.getData().get("id").equals(id)) {
                            isNotificationMatching = true;
                            break;
                        }
                    }

                    if (!isNotificationMatching) {
                        postDataToFirebaseServer(data);
                    }

                } else {
                    Log.d(TAG, "Error getting documents: ", task.getException());
                }
            }
        });


        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.app_logo)
                .setContentText(description)
                .setAutoCancel(true)
                .setSound(defaultSoundUri);
        builder.setContentIntent(pendingIntent);

        NotificationManager noti = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        noti.notify(new Random().nextInt(), builder.build());
    }

    private void postDataToFirebaseServer(Map<String, String> data) {

        Map<String, Object> postDataMap = new HashMap<>();
        postDataMap.put("title", data.get("title"));

        postDataMap.put("timestamp", FieldValue.serverTimestamp());


        mFirestore.collection("notifications").add(postDataMap).addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
            @Override
            public void onSuccess(DocumentReference documentReference) {
//                Toast.makeText(getA.this, "Success", Toast.LENGTH_SHORT);
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                String error = e.getMessage();
//                Toast.makeText(Testing.this, "Failed", Toast.LENGTH_SHORT);
            }
        });

    }

    private void sendNotificationAPI26(RemoteMessage remoteMessage) {
        Intent intent = new Intent(this, Dashboard.class);
        intent.putExtra("notificationFragment", "showNotifications");
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

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

        String title = data.get("title");

        mFirestore.collection("notifications").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        if (document.getData().get("id").equals(id)) {
                            isNotificationMatching = true;
                            break;
                        }
                    }

                    if (!isNotificationMatching) {
                        postDataToFirebaseServer(data);
                    }

                } else {
                    Log.d(TAG, "Error getting documents: ", task.getException());
                }
            }
        });

        NotificationHelper helper;
        Notification.Builder builder;

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        helper = new NotificationHelper(this);
        builder = helper.getLootBoxNotification(title, defaultSoundUri);
        helper.getManager().notify(new Random().nextInt(), builder.build());
        builder.setContentIntent(pendingIntent);

    }
}

This is your code这是你的代码

NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.app_logo)
                .setContentText(description)
                .setAutoCancel(true)
                .setSound(defaultSoundUri);
        builder.setContentIntent(pendingIntent);

Here you have set is as你在这里设置的是

setAutoCancel(true);

This method will cancel all the notification while your app is in foreground.当您的应用程序处于前台时,此方法将取消所有通知。 Set it to false.将其设置为假。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM