Android 8.1.0 MainActivity call without click on notification

Viewed 20

I have shown push notification using firebase but I faced one issue - when app is open and any notification received then Main Activity oncreate() method automatically call without click on notification, so please anyone can help me for resolved this.

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
      pendingIntent = PendingIntent.getActivity(this, (int) (Math.random() * 100), intent, PendingIntent.FLAG_IMMUTABLE);
} else {
      pendingIntent = PendingIntent.getActivity(this, (int) (Math.random() * 100), intent, PendingIntent.FLAG_UPDATE_CURRENT|PendingIntent.FLAG_IMMUTABLE);
}

            String channelId = "channel-01";
            String channelName = "Channel Name";
            int importance = NotificationManager.IMPORTANCE_HIGH;

            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle(jsonObject.getString("title"))
                    .setContentText(jsonObject.getString("body"))
                    .setAutoCancel(true)
                    .setNumber(jsonObject.getInt("badge"))
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setPriority(NotificationCompat.PRIORITY_HIGH)
                    .setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL)
                    .setContentIntent(pendingIntent);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                notificationBuilder.setSmallIcon(R.drawable.ic_stat_notification);
                notificationBuilder.setColor(getResources().getColor(R.color.sky_blue));
                notificationBuilder.setFullScreenIntent(pendingIntent, true);
            } else {
                notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
            }

            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                notificationManager.createNotificationChannelGroup(new NotificationChannelGroup("chats", "Messages"));
                NotificationChannel mChannel = new NotificationChannel(channelId, channelName, importance);
                mChannel.setDescription("description");
                mChannel.setShowBadge(true);
                mChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
                notificationManager.createNotificationChannel(mChannel);
            }
            int num = (int) System.currentTimeMillis();
            notificationManager.notify(num, notificationBuilder.build());
1 Answers

you can do the same using

BroadcastReceiver (android.content.BroadcastReceiver)

And background services (android.app.Service)

Related