Ad

Notification Is Not Shown

- 1 answer

I cant make this simple notification to appear, I kept looking for tutorials and they all provide the same code.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel channel = new NotificationChannel("hgfyjdgh", "Mychannel", NotificationManager.IMPORTANCE_DEFAULT);
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

public void click(View v) {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "id");
    builder.setSmallIcon(R.drawable.img)
            .setContentTitle("Uber Driver")
            .setContentText("أنت الان متصل")
            .setPriority(NotificationCompat.PRIORITY_HIGH);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(23, builder.build());
    Toast.makeText(this, "done", Toast.LENGTH_LONG).show();

}
Ad

Answer

You need to have the same channel on NotificationChannel creation and when you notifiy

Create notification channel name

String NOTIFICATION_CHANNEL_ID = "com.appname.example";

Register it

NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "MyChannelName", NotificationManager.IMPORTANCE_DEFAULT);

Notify it

notificationManager.notify(NOTIFICATION_CHANNEL_ID, builder.build());
Ad
source: stackoverflow.com
Ad