简体   繁体   中英

Android Notification Showing but not popping up on the screen

I am trying to use Notifications in Android. The problem that I am facing is that the notification appears as follows(the black one on the top left): 在此处输入图片说明

I want it to pop up just like the WhatsApp notification pop up. What should I do. Currently I am using following code:

public void displayNotification(String title,String body) {
    Intent intent = new Intent(context, ViewEventsActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(context);
    taskStackBuilder.addNextIntentWithParentStack(intent);

    PendingIntent pendingIntent = taskStackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);

    Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.logo);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context,Utils.CHANNEL_ID)
            .setSmallIcon(R.drawable.logo)
            .setLargeIcon(icon)
            .setContentTitle("SRC:"+title)
            .setContentText(body)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .setCategory(NotificationCompat.CATEGORY_ALARM);

    NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel=new NotificationChannel(CHANNEL_ID,CHANNEL_NAME,NotificationManager.IMPORTANCE_DEFAULT);

        notificationChannel.setDescription(CHANNEL_DESC);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{100,200,300,400,500,400,300,200,400});
        notificationManager.createNotificationChannel(notificationChannel);
    }

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

How to achieve my desired task?

I had the same problem today. After lots of tries I found a solution. You just have to add priority and defaults. Here is example with custom view.

NotificationCompat.Builder(context)
            .setCustomContentView(createView())
            .setDefaults(NotificationCompat.DEFAULT_ALL)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setChannelId(CHANNEL_ID)
            .setSmallIcon(R.drawable.icon_notification)
            .build()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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