How to make Notification appear on the phone for Android Studio?
Asked Answered
T

3

5

I want to create a push notification where Admin can send notification to all users. I found a tutorial and follow it but it doesn't work. I'm not sure where I did wrong but I got error that said

Developer Warning for package "... " Failed to post notification on channel "null"

 b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tittle = ed1.getText().toString().trim();
                String subject = ed2.getText().toString().trim();
                String body = ed3.getText().toString().trim();

                NotificationManager notif = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                Notification notify = new Notification.Builder
                        (getApplicationContext()).setContentTitle(tittle).setContentText(body).
                        setContentTitle(subject).setSmallIcon(R.drawable.ps).build();

                notify.flags |= Notification.FLAG_AUTO_CANCEL;
                notif.notify(0, notify);
            }
        });

Tartarous answered 12/11, 2019 at 10:56 Comment(6)
did yu using FCM or some other concept to send notificationRadiotelephony
For push notification, you need to use push services like Firebase Cloud Messaging firebase.google.com/docs/cloud-messaging, but I will recommend you to use OneSignal onesignal.com which is easy to implement using API calls.Rubefaction
@Radiotelephony no. I just want to create a simple notification but i can send to all usersTartarous
@OMiShah will check it! thank youTartarous
than you have to implement FCM messaging service blog.mindorks.com/pushing-notifications-in-android-using-fcm refer this linkRadiotelephony
@Radiotelephony I have questions, if i implement the FCM Messaging service can I send the notification through apps rather than i have to go to firebase console to send the notification? I want to make a notification than can be send by phoneTartarous
E
18

After Oreo SDK you have to create Notification channel in order to show a notification, check this method for reference:

/**
 *
 * @param context
 * @param title  --> title to show
 * @param message --> details to show
 * @param intent --> What should happen on clicking the notification
 * @param reqCode --> unique code for the notification
 */

public void showNotification(Context context, String title, String message, Intent intent, int reqCode) {
    SharedPreferenceManager sharedPreferenceManager = SharedPreferenceManager.getInstance(context);

    PendingIntent pendingIntent = PendingIntent.getActivity(context, reqCode, intent, PendingIntent.FLAG_ONE_SHOT);
    String CHANNEL_ID = "channel_name";// The id of the channel.
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(R.mipmap.notification_logo)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "Channel Name";// The user-visible name of the channel.
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        notificationManager.createNotificationChannel(mChannel);
    }
    notificationManager.notify(reqCode, notificationBuilder.build()); // 0 is the request code, it should be unique id

    Log.d("showNotification", "showNotification: " + reqCode);
}

How to use this method:

    int reqCode = 1;
    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    showNotification(this, "Title", "This is the message to display", intent, reqCode);
Emanuelemanuela answered 12/11, 2019 at 11:34 Comment(5)
do I have to implement it on the same page of my class?Tartarous
It's a public function, you are passing context in the parameter you can use this method to show notification in Android system.Emanuelemanuela
I have question. I have notification() on my onCreate . How to pass (Context context, String title, String message, Intent intent, int reqCode) inside my notification on my onCreate ?Tartarous
I've added the implementation.Emanuelemanuela
If you are using Firebase push notification service then you can show notification on push received by passing the dataEmanuelemanuela
S
0
     public void sendNotification (String message, String title ){

        Intent intent = new Intent(getApplicationContext(), CampaignActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent,
                PendingIntent.FLAG_IMMUTABLE);


        String channelId = "some_channel_id";
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.mipmap.ic_launcher_round)
//                        .setContentTitle(getString(R.string.app_name)
                        .setContentTitle(title)
                        .setContentText(message)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

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

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            assert notificationManager != null;
            notificationManager.createNotificationChannel(channel);
        }

        assert notificationManager != null;
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
Sniffy answered 29/11, 2022 at 22:15 Comment(0)
P
0

If you want to send multiple notifications at same instance .

 // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is not in the Support Library.
    val mBuilder = NotificationCompat.Builder(LocalContext.current,     "channel_id")
    mBuilder.setSmallIcon(android.R.drawable.stat_notify_chat);
    mBuilder.setPriority(NotificationCompat.PRIORITY_MAX);
    mBuilder.setGroup("dicer app group")

for (item in itemList) {
    
    mBuilder.setContentTitle(item.name);
    mBuilder.setContentText(item.details);
   
   
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val name = ("channel_name")
            val descriptionText = "channel_description"
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val channel = NotificationChannel("channel_id", name, importance).apply {
                description = descriptionText
            }

           val notificationManager = getSystemService(LocalContext.current, NotificationManager::class.java) as NotificationManager
        
           notificationManager.createNotificationChannel(channel)
           notificationManager.notify(item.id, mBuilder.build())

        }
 
Photoflood answered 10/6, 2024 at 17:14 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.