Android-Notification doesn't appear

前端 未结 1 1458
陌清茗
陌清茗 2021-01-15 00:41

This is a code for creating a notification by click a button and then turn to another class. The second class it totally empty. I don\'t know why it doesn\'t appear even it

相关标签:
1条回答
  • 2021-01-15 01:16

    As you have stated your compiled sdk version is 26. You need to set Channel of your notification before posting it. So it will be like

    NotificationManager notification_manager = (NotificationManager) this
                    .getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        String chanel_id = "3000";
        CharSequence name = "Channel Name";
        String description = "Chanel Description";
        int importance = NotificationManager.IMPORTANCE_LOW;
        NotificationChannel mChannel = new NotificationChannel(chanel_id, name, importance);
        mChannel.setDescription(description);
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.BLUE);
        notification_manager.createNotificationChannel(mChannel);
        notification_builder = new NotificationCompat.Builder(this, chanel_id)
    } else {
        notification_builder = new NotificationCompat.Builder(this);
    }
    notification_builder.setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Notification Title")
            .setContentText("Notification Body")
            .setAutoCancel(true)
            .setContentIntent(pending_intent);
    

    For more please visit Developer Site

    0 讨论(0)
提交回复
热议问题