Calling a notification in onReceive of broadcast receiver - java

public class MyBroadcaseciver extends BroadcastReceiver {
MediaPlayer mymedia;
#Override
public void onReceive(Context context, Intent intent) {
mymedia=MediaPlayer.create(context,R.raw.alarm);
mymedia.start();
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
}
}
The code above is my broadcast receiver, which plays a song when it fires. This is working as expected, however I wish to call a push notification to pop here and also my notification isn't working here.
Notification.Builder bulider = new Notification.Builder(this)
.setContentTitle("Rainfall Alert")
.setContentText("Todays Rain");
Notification notification = bulider.build();
NotificationManager manager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0,notification);
Help is appreciated.

public void sendNotification(String title, String body, Intent intent,int pushid) {
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(body))
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSoundUri);
if(intent!=null) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
notificationBuilder.setContentIntent(pendingIntent);
}
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
call this funcation in onrecive method
Intent intent = new Intent(context, Mainscreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
sendNotification("title","message", intent, 0);

public class MyBroadcaseciver extends BroadcastReceiver {
MediaPlayer mymedia;
#Override
public void onReceive(Context context, Intent intent) {
mymedia=MediaPlayer.create(context,R.raw.alarm);
mymedia.start();
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
Intent intent = new Intent(context, Mainscreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
sendNotification("title","message", intent, 0);
}
public void sendNotification(String title, String body, Intent intent,int pushid) {
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(title)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(body))
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSoundUri);
if(intent!=null) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
notificationBuilder.setContentIntent(pendingIntent);
}
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}

Related

How can I know which button was clicked in the notification?

I am developing an application, I have 2 buttons in an on the notification. How can I know which button the user has clicked?
This my Notification codes;
public void NotificationSettings(Context context){
Intent stateIntent = new Intent(context, MyBroadcastReceiver.class);
stateIntent.putExtra("id", 100);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context, 0, stateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder = new NotificationCompat.Builder(context, "access2020")
.setSmallIcon(R.drawable.ic_baseline_add_alert_24)
.setContentTitle("Academy Notification")
.setContentText("Hey this is an important notifications")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.addAction(R.drawable.ic_baseline_add_alert_24, "Set Active", pendingIntent)
.addAction(R.drawable.ic_baseline_add_alert_24,"Dismiss", pendingIntent);
notificationManager = NotificationManagerCompat.from(context);
}
and My Broadcast
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "test", Toast.LENGTH_SHORT).show();
int notificationId = intent.getIntExtra("id", 0);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.cancel(notificationId);
}
You should pass an identifier to the intent as an extra and then retrieve in your BroadcastReceiver.
public void NotificationSettings(Context context){
// put an extra identifier for Set Active Action
Intent setActiveStateIntent = new Intent(context, MyBroadcastReceiver.class);
setActiveStateIntent.putExtra("id", 100);
setActiveStateIntent.putExtra("action", "Action.SetActive");
PendingIntent setActivePendingIntent =
PendingIntent.getBroadcast(context, 0, setActiveStateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// put an extra identifier for Dismiss
Intent dismissStateIntent = new Intent(context, MyBroadcastReceiver.class);
dismissStateIntent.putExtra("id", 100);
dismissStateIntent.putExtra("action", "Action.Dismiss");
PendingIntent dismissPendingIntent =
PendingIntent.getBroadcast(context, 0, dismissStateIntent, PendingIntent.FLAG_UPDATE_CURRENT);
builder = new NotificationCompat.Builder(context, "access2020")
.setSmallIcon(R.drawable.ic_baseline_add_alert_24)
.setContentTitle("Academy Notification")
.setContentText("Hey this is an important notifications")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.addAction(R.drawable.ic_baseline_add_alert_24, "Set Active", setActivePendingIntent)
.addAction(R.drawable.ic_baseline_add_alert_24,"Dismiss", dismissPendingIntent);
notificationManager = NotificationManagerCompat.from(context);
}
Then in your BroadcastReceiver you can do the following:
public void onReceive(Context context, Intent intent) {
if(intent.getStringExtra("action").equals("Action.Dismiss")) {
// perform your dismiss action
} else if (intent.getStringExtra("action").equals("Action.SetActive")) {
// perform your set active logic
} else {
// handle invalid action
}
}
I solved with this way but still i am not sure it is the right way
public void NotificationSettings(Context context){
Intent stateIntent0 = new Intent(context, MyBroadcastReceiver.class);
Intent stateIntent1 = new Intent(context, MyBroadcastReceiver.class);
stateIntent0.putExtra("id", 100);
stateIntent1.putExtra("id", 200);
PendingIntent pendingIntent0 =
PendingIntent.getBroadcast(context, 0, stateIntent0, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent1 =
PendingIntent.getBroadcast(context, 1, stateIntent1, PendingIntent.FLAG_UPDATE_CURRENT);
builder = new NotificationCompat.Builder(context, "lemubitA")
.setSmallIcon(R.drawable.ic_baseline_add_alert_24)
.setContentTitle("Lemubit Academy Notification")
.setContentText("Hey this is an important notifications")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.addAction(R.drawable.ic_baseline_add_alert_24, "Set Active", pendingIntent0)
.addAction(R.drawable.ic_baseline_add_alert_24,"Dismiss", pendingIntent1);
notificationManager = NotificationManagerCompat.from(context);
}
AND this my Broadcast
int notificationId = intent.getIntExtra("id", 0);
Toast.makeText(context, notificationId+"", Toast.LENGTH_SHORT).show();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.cancel(notificationId);

Implement intent method after clicking the notification (NotificationCompat.Builder)

I'm developing a notification method. it's working successfully. but how can I implement Onclick method to navigate a specific activity?
private void notification() {
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O)
{
NotificationChannel channel=new NotificationChannel("n","n", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager=getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel);
}
NotificationCompat.Builder builder=new NotificationCompat.Builder(this,"n")
.setContentText("Alert")
.setSmallIcon(R.drawable.ic_notifications)
.setAutoCancel(true)
.setContentText("Check your fit Room Request");
NotificationManagerCompat managerCompat=NotificationManagerCompat.from(this);
managerCompat.notify(999,builder.build());
}
Intent intent = new Intent(this, YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Then
NotificationCompat.Builder builder=new NotificationCompat.Builder(this,"n")
.setContentIntent(pendingIntent)
.....

Java android: pushing notification won't work

When i run this on my android app to try and get a notification with the command: sendNotification("test","title",1);
i get an error: E/NotificationManager: notifyAsUser: tag=null, id=1, user=UserHandle{0}
private void sendNotification(String message, String title, int id) {
Intent intent = new Intent(this, Game.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */,
intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.maintitle)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(this.NOTIFICATION_SERVICE);
notificationManager.notify(id /* ID of notification */,
notificationBuilder.build());
}
You must set Channel id for new android versions.
Create your notification this way
private final static String CHANNEL_ID = "my_notification";
private void sendNotification(String message, String title, int id) {
Intent intent = new Intent(this, Game.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */,
intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.maintitle)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, "news_notification", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(id /* ID of notification */,
notificationBuilder.build());
}
Set CHANNEL_ID in NotificationCompat.Builder and create channel

How can i open Activity when notification click

I need use notification with click event, i have notification method but this method don't open my activity.
My code:
private void sendNotification(String msg) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle("EXX")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setOngoing(true);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
is this possible,
thanks.
Yes, it is possible.
Change Your method, like that;
private void sendNotification(String msg) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("yourpackage.notifyId", NOTIFICATION_ID);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle("EXX")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.addAction(getNotificationIcon(), "Action Button", pIntent)
.setContentIntent(pIntent)
.setContentText(msg)
.setOngoing(true);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
and add your mainactivity
NotificationManager notificationmanager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
this code working.
Hey #John it's very simple
you just have to do
Intent intent = new Intent(this, ResultActivity.class);
...
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent pendingIntent = TaskStackBuilder.create(getApp())
.addNextIntent(intent)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
And set pending Intent in mBuilder
private void sendNotification(String msg) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setContentTitle("EXX")
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(msg))
.setContentText(msg)
.setOngoing(true);
.setContentIntent(pendingIntent)
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
and you done :)

Start an Activity when clicking a notification

I'm trying to start an activity by clicking on a notification but that only works when I'm already in the app.
Here's the code I think has to do with it:
private void sendNotification(String body) {
Intent intent = new Intent(ImpFirebaseMessagingService.this, codeActivity.class);
intent.putExtra("body", body);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Implementation TFA")
.setContentText(body)
.setAutoCancel(true)
.setSound(notificationSound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
The following works in my app:
private void sendReminderNotification(Context context) {
Resources resources = context.getResources();
Intent notificationIntent = new Intent(context, TitleActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder =
new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
.setLargeIcon(BitmapFactory.decodeResource(resources, R.drawable.my_diary_launcher_icon))
.setContentTitle("MyDiary")
.setContentText("Reminder to input entry.")
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_MAX)
.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(NOTIFICATION_ID, builder.build());
}
Use pendingIntent to pass data of intent:
private void sendReminderNotification(Context context) {
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Implementation TFA")
.setContentText(body)
.setAutoCancel(true)
.setSound(notificationSound);
Intent intent = new Intent(ImpFirebaseMessagingService.this, codeActivity.class);
intent.putExtra("body", body);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}

Categories