How to create a notification when my app receive a broadcast message

by Asad Faraz   Last Updated October 10, 2019 02:26 AM

My app simply receives a broadcast from another app. I am new to broadcast receiver so I am facing problems. I want that when my app receive a broadcast message then in the notification panel a notification appears. I tried a lot but find nothing on the internet.

I tried youtube videos and StackOverflow but I found nothing.

public class MyReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    Toast.makeText(context, "EBR triggered", Toast.LENGTH_LONG).show();

// The following code doesn't work. I copy this from StackOverflow// PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, 0);

    Notification.Builder builder = new Notification.Builder(context);

    builder.setAutoCancel(false);
    builder.setTicker("Ticker text");
    builder.setContentTitle("Content of Notification");
    builder.setContentText("You have a new message");
    builder.setSmallIcon(R.mipmap.ic_launcher);
    builder.setContentIntent(pendingIntent);
    builder.setOngoing(true);
    builder.setSubText("This is subtext...");  
    builder.setNumber(100);
    builder.build();

    // add as notification
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    manager.notify(0, builder.build());


}


}

No errors and I was expected that there will be a notification icon appears when broadcast receives but nothing happened.



Answers 1


Here is a simple example of broadcast receiving.

App 1 (sender)

Intent intent = new Intent("MY_NOTIFICATION");
intent.setComponent(
    new ComponentName("com.example.stackoverflow", "com.example.stackoverflow.MyReceiver")
);
intent.putExtra("data","Notice me senpai!");
sendBroadcast(intent);

App 2 (receiver)

AndroidManifest.xml:

...
<receiver android:name=".MyReceiver">
    <intent-filter>
        <action android:name="MY_NOTIFICATION"/>
    </intent-filter>
</receiver>

MyReceiver.java:

public void onReceive(Context context, Intent intent) {
        String data = intent.getStringExtra("data");
        Toast.makeText(context, data, Toast.LENGTH_LONG).show();

        NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
        NotificationChannel channel = new NotificationChannel("CHANNEL_ID", "name", NotificationManager.IMPORTANCE_DEFAULT);
        channel.setDescription("description");
        notificationManager.createNotificationChannel(channel);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "CHANNEL_ID")
                .setSmallIcon(R.drawable.ic_launcher_background)
                .setContentTitle("textTitle")
                .setContentText(data)
                .setPriority(NotificationCompat.PRIORITY_DEFAULT);
        notificationManager.notify(1, builder.build());
    }

Please note that there are Broacast limitations since Android 8. So you need to to provide the explicit class for handling i.e setComponent param along with action

MrBinWin
MrBinWin
October 10, 2019 02:25 AM

Related Questions


Updated January 02, 2018 02:26 AM

Updated September 03, 2017 06:26 AM

Updated November 16, 2017 14:26 PM

Updated July 21, 2019 22:26 PM