I am trying to open a web page when a notification is clicked:
public void shownNotification(String title, String message){
Intent notifyIntent = new Intent(this, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
notifyIntent.setData(Uri.parse("htp://www.google.com"));
PendingIntent pendingItent = PendingIntent.getActivities(this, 0, new Intent[] {notifyIntent }, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingItent)
.build();
notification.defaults |= Notification.DEFAULT_SOUND;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
}
I get the notification with out a problem but when I click it, it just bring me to the application.
Change your notifyIntent to:
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yoururl.com"));
Then your code will be something like:
public void shownNotification(String title, String message){
Intent notifyIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.yoururl.com"));
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingItent = PendingIntent.getActivities(this, 0, new Intent[] {notifyIntent }, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingItent)
.build();
notification.defaults |= Notification.DEFAULT_SOUND;
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notification);
}
Related
I want to change the icon of my notification, i have tried the NotificationCompat.Builder's method setSmallIcon(int), but the icon don't change (screenshot of my app) (the icon i want to use), what can i do?
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
Intent intent = new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setSmallIcon(R.mipmap.ic_stat_onesignal_default)
.setLights(Color.WHITE, 300, 100)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(1, builder.build());
I'am using FCM for getting new notification which I am able to show without any problem but when i open the app all my notification disappeared
this my code for creating new notification
final NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
mContext,mContext.getString(R.string.default_notification_channel_id));
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setStyle(inboxStyle)
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(R.drawable.notification_icon)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.build();
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
CharSequence name = "my_channel";
String Description = "This is my channel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(mContext.getString(R.string.default_notification_channel_id), name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
}
int id = (int) System.currentTimeMillis();
notificationManager.notify(id, notification);
when click on a notification it opens an activity with extra data in
my intent
Intent resultIntent = new Intent(getApplicationContext(), MainActivity.class);
resultIntent.putExtra("message", "wwwwwwww");
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
mContext,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
);
now when i click on any notification it opens MainActivity with some extra value on which Main activity depends.
But when i click on a notification and it launches the activity all other notification disappeared.So, how can i prevent that and hide only clicked notification so user can see other notification
not quite sure whether it'll work or not but can u please try with setting the request code unique rather than 0.
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
replace 0 with the following
PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,(int) System.currentTimeMillis(),intent,PendingIntent.FLAG_UPDATE_CURRENT);
try this
**.setPriority(NotificationManager.IMPORTANCE_HIGH)**
Notification notification;
notification = mBuilder.setSmallIcon(icon).setTicker(title).setWhen(0)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(resultPendingIntent)
.setSound(alarmSound)
.setStyle(new NotificationCompat.BigTextStyle().bigText(message))
.setWhen(getTimeMilliSec(timeStamp))
.setSmallIcon(ICON)
.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), icon))
.setContentText(message)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(NotificationManager.IMPORTANCE_HIGH)
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(num, notification);
I am trying to make Notification Reply like whatsapp.
here is my code
public void sendNotification(String msg, Intent i, ContactModel contact)
{
i.putExtra("message", msg);
i.putExtra("contact", new Gson().toJson(contact));
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mNotificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
String replyLabel = "Reply";
RemoteInput remoteInput = new RemoteInput.Builder("KEY_REPLY")
.setLabel(replyLabel)
.build();
PendingIntent contentIntent = PendingIntent.getActivity(context, 100, i,
PendingIntent.FLAG_UPDATE_CURRENT);
Bundle bundle = new Bundle();
bundle.putBoolean("reply", true);
NotificationCompat.Action replyAction = new NotificationCompat.Action.Builder(
R.drawable.ic_phone, replyLabel, contentIntent)
.addRemoteInput(remoteInput)
.addExtras(bundle)
.setAllowGeneratedReplies(true)
.build();
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher))
.setContentTitle(contact.getName())
.setDefaults(Notification.DEFAULT_SOUND)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Content Hidden"))
.setContentText(msg)
.addAction(replyAction)
;
mBuilder.setContentIntent(contentIntent);
mNotificationManager.cancel(Constants.PUSH_ID);
mNotificationManager.notify(Constants.PUSH_ID, mBuilder.build());
}
Now my notification shows "Message" and a "Reply" Button.
The problem i am facing is, i can't differentiate wether user chose Reply button or tried to open the notification.
My main intent is
Intent myIntent = new Intent(this, NotificationActivity.class);
What i want is,
When a user click on "Reply" Button it opens
Intent myIntent = new Intent(this, NotificationActivity.class);
When a user click on notification itself it opens
Intent myIntent = new Intent(this, MainActivity.class);
Even if thats not possible , how can i figure out which action was performed.
Try to add
setContentIntent(new Intent(this, MainActivity.class))
Additional info here
You should create a pending intent for each case , one for reply button and another for your message notification.
addAction() define action button for your notificaion, and setContentIntent(pendingIntent) do the same for your core message.
.... your code
Intent messageIntent = new Intent(this, MainActivity.class);
PendingIntent messagePendingIntent = PendingIntent.getActivity(context, 200, messageIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher))
.setContentTitle(contact.getName())
.setDefaults(Notification.DEFAULT_SOUND)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("Content Hidden"))
.setContentText(msg)
.setContentIntent(messagePendingIntent) // some change here
.addAction(replyAction)
Hope this helps.
Sorry for my english.
What I understand from your statement is: you are expecting two different intents for two different actions so, I'm not confirm that it will solve your problem, but this is what I tried.
public void createNotification() {
Intent intent = new Intent(this, NotificatioTest.class);
PendingIntent pIntent = PendingIntent.getActivity(this,(int) System.currentTimeMillis(), intent, 0);
Intent mIntent = new Intent(this, MainActivity.class);
PendingIntent mPIntent = PendingIntent.getActivity(this,(int)System.currentTimeMillis(),mIntent,0);
Notification noti = new Notification.Builder(this)
.setContentTitle("Your Title")
.setContentText("Your text content")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.addAction(R.drawable.ic_launcher, "Reply", mPIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
The change is I have used two different PendingIntent as setContentIntent(pIntent) and addAction(R.drawable.ic_launcher,"Reply",mPIentent).
Output:
https://youtu.be/vVKVMboHEOA
I'm trying to create Notification using AppCompat inside Fragment.
I create see nothing.
Code:
private void createNotification()
{
Context context = getActivity().getApplicationContext();
Intent notificationIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ymk_ya_logo)
.setAutoCancel(false)
.setContentTitle("notify")
.setContentText("test");
Notification notification = builder.build();
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(777, notification);
}
Try changing your last bit to this instead:
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, builder.build());
I use this code to show a notification:
public static void ShowCustomNotification(Context context,String notificationTitle, String notificationMessage)
{
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// intent triggered, you can add other intent for other actions
Intent intent = new Intent(context, ResultActivity.class);
intent.putExtra("title", notificationTitle);
intent.putExtra("body", notificationMessage);
intent.setFlags(PendingIntent.FLAG_UPDATE_CURRENT
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
Notification mNotification = new NotificationCompat.Builder(context)
.setContentTitle(notificationTitle)
.setSmallIcon(R.drawable.office_girl)
.setContentIntent(pIntent)
.setSound(soundUri)
.build();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotification);
}
When the user clicks the Notification the activity ResultActivity.class is invoked and everything is fine.
My question is how can i clear the notification on the status bar when a user click on a "Close" button from inside ResultActivity? in case the user click "Back" i dont want to clear the notification.
Thanks.
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(YOUR_NOTIFICATION_ID);