I get notification in my application through BroadcastReceiver, The question is , How can I parse data to an activity and make a dialog with the received data ?
this is my code but when I click on the notification , nothing happens :
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
notificationIntent.setData(Uri.parse(link));
PendingIntent pending = PendingIntent.getActivity(ctx, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
Notification myNotification = new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.ic_launcher).setAutoCancel(false).setLargeIcon(remote_picture)
.setContentTitle(onvan).setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg).setContentIntent(pending).build();
notificationManager.notify(1, myNotification);
I've some variablese like link , msg, onvan that contains my data and I need to send these variables to an activity and make a dialog .
How can I do so ?
Try this:
Intent notifyIntent = new Intent(context,YourActivityClassHere.class);
notifyIntent.setFlag(Intent.FLAG_ACTIVITY_NEW_TASK);
//UNIQUE_ID if you expect more than one notification to appear
PendingIntent intent = PendingIntent.getActivity(SimpleNotification.this, UNIQUE_ID,
notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
just make the PendingIntent open up one of your Activities and have your Activity be complete transparent and just open a Dialog.
EDIT: IF you want to open an Activity from notification click event:
Assuming that notif is your Notification object:
Intent notificationIntent = new Intent(this.getApplicationContext(), ActivityToStart.class);
PendingIntent contentIntent = PendingIntent.getActivity(this.getApplicationContext(), 0, notificationIntent, 0);
notif.contentIntent = contentIntent;
For more detail visit here. Android - Prompt Dialog window when touch on Notification
You can send data from your notification to another Activity using method putExtra and put this
PendingIntent pending = PendingIntent.getActivity(ctx, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT));
inplace of this
PendingIntent pending = PendingIntent.getActivity(ctx, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);`
Related
#RequiresApi(api = Build.VERSION_CODES.M)
public void showNotification(int playPauseBtn) {
Intent intent = new Intent(this, ExoActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_MUTABLE);
Intent prevIntent = new Intent(this, NotificationReceiver.class).setAction(ACTION_PREVIOUS);
PendingIntent prevPendingIntent = PendingIntent.getBroadcast(this, 0, prevIntent,
PendingIntent.FLAG_MUTABLE);
Intent playIntent = new Intent(this, NotificationReceiver.class).setAction(ACTION_PLAY);
PendingIntent playPendingIntent = PendingIntent.getBroadcast(this, 0, playIntent,
PendingIntent.FLAG_MUTABLE);
Intent nextIntent = new Intent(this, NotificationReceiver.class).setAction(ACTION_NEXT);
PendingIntent nextPendingIntent = PendingIntent.getBroadcast(this, 0, nextIntent,
PendingIntent.FLAG_MUTABLE);
Bitmap picture = BitmapFactory.decodeResource(getResources(), HelperClass.getlist().get(position).getImg());
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID_2)
.setSmallIcon(HelperClass.getlist().get(position).getImg()) //get thumbnail will have small icon pic
.setLargeIcon(picture)
.setContentTitle(HelperClass.getlist().get(position).getName())
.addAction(R.drawable.ic_baseline_skip_previous_24, "Previous", prevPendingIntent)
.addAction(R.drawable.ic_baseline_play_arrow_24, "Play", playPendingIntent)
.addAction(R.drawable.ic_baseline_skip_next_24, "Next", nextPendingIntent)
//.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
//.setMediaSession(mediaSession.getSessionToken()))
.setPriority(Notification.PRIORITY_HIGH)
.setC`your text`ontentIntent(contentIntent)
.setOnlyAlertOnce(true).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
This code creates Notification wherever this 'showNotification()' function is called but what if I want to shut the app by removing the notification?
use setDeleteIntent method - when notification is removed you can send some broadcast to all your "contextual" ojects (e.g. Activity, Service)
note that there is also setOngoing method, which will stick your notification and user won't be able to remove it "manually". then you can provide "kill" action button for this notification, which may behave exacly same as one created for setDeleteIntent. in that case don't forget to remove notification when user just leave/exit your app, when "kill" action pressed or in any other "usual" way
I am currently working on a reminders app in which the user gets a notification with the name of the reminder and is then redirected to an activity which contains the text of the reminder in detail.
I am however, only able to redirect to the same activity each time.
I am using this code :
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
notification.setLatestEventInfo(this, title, text, contentIntent);
So this redirects to the MainActivity on clicking the notification. I would like to redirect to a separate screen and then based on a key value display a text on that activity.
How do I achieve this ?
Thanks
Just change the PendingIntent using another Activity and/or append extra information on the Intent you are using to create the PendingIntent:
Intent launchIntent = new Intent(this, AnotherActivity.class)
launchIntent.putExtra("myKey", "myValue");
//....
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, launchIntent , 0);
notification.setLatestEventInfo(this, title, text, contentIntent);
And than, in you Activity's onCreate():
//...
getIntent().getStringExtra("myKey")
//do your stuff..
Just pass the value in the intent . eg.
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);
contentIntent.putExtra("phone", value);
contentIntent.putExtra("name", value);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, contentIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
Question has already been anwered and accepted, but here's another method:
int mId = 1;
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, TwoActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(this)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Title")
.setContentText("Content")
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true)
.setNumber(1);
NotificationManager manager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(mId, builder.build());
I am trying to open dialog on click of notification but unable to do this:
Here's my code:
Intent in = new Intent(context, SnoozeEvent.class);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent Sender = PendingIntent.getBroadcast(context, 0, intent, 0);
manager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notification = new Notification(R.drawable.icon, "Wake up alarm", System.currentTimeMillis());
notification.setLatestEventInfo(context, "Hanuman Chalisa", "Wake Up...", Sender);
manager.notify(1, notification);
The error is that you point the PendingIntent to an intent that doesn't exist (it points to an intent called "intent" - you created an intent called "in").
Replace the following line:
PendingIntent Sender = PendingIntent.getBroadcast(context, 0, intent, 0);
With this (so it points to the intent you created):
PendingIntent Sender = PendingIntent.getBroadcast(context, 0, in, 0);
If you do that, then everything should work fine.
I am developing a small app in which I want a background service to send a notification and when the user clicks the notification it must launch the appropriate activity. I am posting the code here for the notification and my problem is that the notification gets displayed on the status bar but when i click it it does not launch the activity. Can somebody suggest where I am going wrong. Please help.
NotificationManager notificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"A New Message!", System.currentTimeMillis());
Intent notificationIntent = new Intent(NotificationService.this,
com.jeris.android.MetroActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent
.getService(NotificationService.this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
notification.setLatestEventInfo(NotificationService.this, "Bullion",
"Rates for "+ parsedDate+"has not been updated. Go to app to check rates",
pendingIntent);
notificationManager.notify(11, notification);
Replace
PendingIntent.getService(NotificationService.this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
With
PendingIntent.getActivity(NotificationService.this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
Notice the getService is replaced with getActivity.
What I have below is some notification code. What I would like to achieve with my notification code is when the notification is clicked, it will take the user to a particular class. Here is my notification code:
final int NOTIF_ID = 1234;
NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.icon, "New Offers CLOSEBY!", System.currentTimeMillis());
PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, intentTHING.class), 0);
note.setLatestEventInfo(this, "You have 5 new offers", "Please CLICK THIS Notification to see what is avaliable!", intent);
notifManager.notify(NOTIF_ID, note);
I'm assuming that when the notification is clicked, this code here is supposed to take you to the declared class:
PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, intentTHING.class), 0);
However, it does not, the notification does appear, but I cannot see the class I have referenced in my code (i.e when clicked it does not take me to the class I want it to). I have the notification popup currently after a button has been pressed in another class. Once the user clicks on the notification it should push that screen and replace it with the intentTHING.class. Any ideas as to what I may be missing?
Set pending Intent like this:
PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, intentTHING.class), 0);
note .contentIntent = intent;
note.setLatestEventInfo(this, "You have 5 new offers", "Please CLICK THIS Notification to see what is avaliable!", intent);
notifManager.notify(NOTIF_ID, note);
Am not sure but can try it out by doing this..
PendingIntent intent =
PendingIntent.getActivity(context, 0,
new Intent(this, intentTHING.class), PendingIntent.FLAG_UPDATE_CURRENT | Notification.FLAG_AUTO_CANCEL);