By Sabyasachi Sahu


2020-06-30 03:21:18 8 Comments

I am trying to create a push notification for my new app. I tried creating the push notification but it is not showing up. I followed the documentation here: https://developer.android.com/training/notify-user/build-notification

public class MainActivity extends AppCompatActivity {

int notificationID = 1;
String CHANNEL_ID = "food";

private void createNotificationChannel() {
   
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);


    NotificationCompat.Builder builder= new NotificationCompat.Builder(this, "food")
            .setSmallIcon(R.drawable.ic_launcher_background)
            .setContentTitle("Hello")
            .setContentText("What are you doing?")
            .setStyle(new NotificationCompat.BigTextStyle().bigText("What are you doing?"))
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);

    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);


    notificationManager.notify(notificationID, builder.build());

}

}

The resource string XML code where I have defined the data is here:

<resources>
    <string name="app_name">Notifications demo</string>
    <string name="channel_name">food</string>
    <string name ="channel_description">different type of food</string>
    
</resources>

1 comments

@Vlad 2020-06-30 03:27:22

You created a channel with id my_channel_01 but using in notification with id food
Just change this line

NotificationCompat.Builder builder= new NotificationCompat.Builder(this, CHANNEL_ID)

@Sabyasachi Sahu 2020-06-30 03:38:58

Thank you. I changed that now and tried again, but it still does not render. Let me edit that is above code as well.

@Vlad 2020-06-30 03:41:57

@SabyasachiSahu please check if the function createNotificationChannel() is also called in your code before showing notification. It seems is not being called

@Sabyasachi Sahu 2020-06-30 03:52:56

Thank you so much @Vlad this really helps, I somehow missed that.

Related Questions

Sponsored Content

105 Answered Questions

[SOLVED] Close/hide android soft keyboard

23 Answered Questions

[SOLVED] Is there a way to run Python on Android?

29 Answered Questions

[SOLVED] What is 'Context' on Android?

1 Answered Questions

Push notification is not working in android 10

  • 2020-06-15 17:28:26
  • Shadab Azam Farooqui
  • 28 View
  • 0 Score
  • 1 Answer
  • Tags:   android

51 Answered Questions

[SOLVED] Is there a unique Android device ID?

52 Answered Questions

11 Answered Questions

[SOLVED] Proper use cases for Android UserManager.isUserAGoat()?

38 Answered Questions

[SOLVED] How to lazy load images in ListView in Android

17 Answered Questions

[SOLVED] "Debug certificate expired" error in Eclipse Android plugins

Sponsored Content