Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – Notification not showing using NotificationManager

Posted on November 14, 2022

Solution 1 :

There seem to have been a recent update which requires you to run some additional code in order for it to work on newer android versions. So the code should looks something like:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
                    .setSmallIcon(R.drawable.ic_challenge)
                    .setContentTitle(messageTitle)
                    .setContentText(messageBody)
                    .setPriority(Notification.PRIORITY_MAX)
                    .setContentIntent(pendingIntent);

            //to show notification do this

            //Sets ID for the notification

            int mNotificationId = (int) System.currentTimeMillis();
            NotificationManager mNotifyMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
            {
                String channelId = "Tic-Tac-Toe";
                NotificationChannel channel = new NotificationChannel(
                        channelId,
                        "Tic-Tac-Toe",
                        NotificationManager.IMPORTANCE_HIGH);
                mNotifyMgr.createNotificationChannel(channel);
                mBuilder.setChannelId(channelId);
            }

            mNotifyMgr.notify(mNotificationId, mBuilder.build());

Problem :

I’m sending push notifications in my app and want to be able to show them even if the app is already running, so therefore I’m trying to use the onMessageReceived() function. The function runs whenever I send a notification and I can see that the title and body of the notification is correct, so no problems this far. Then I want the notification to pop up on the users device, but for some reason I just can’t get it to work. I have looked at numerous sites and stackoverflow questions and all code basically looks the same, so it’s a bit confusing why it doesn’t work for me.

 @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        String messageTitle = remoteMessage.getNotification().getTitle();
        String messageBody = remoteMessage.getNotification().getBody();
        System.out.println("TITLE_IS: " + messageTitle);
        System.out.println("MESSAGE_BODY: "+ messageBody);

        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(messageTitle)
                .setContentText(messageBody);

        //Sets ID for the notification
        int mNotificationId = (int) System.currentTimeMillis();
        

        NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNotifyMgr.notify(mNotificationId, mBuilder.build());

        System.out.println("Everything went fine");
    }

It never prints the last line (“Everything went fine”), but also don’t give an error so it seems it works even though it didn’t. What is the problem and how do I fix it?

READ  [ANSWERED] android - I want to add multi-language support for my modularly implemented project
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress