I have successfully created a notification thanks to my other question, using NotificationCompat. I want to now just change what the notification does onClick. I'd really just like to have an alert dialog pop up (I've seen some app's do it) but everytime I click it, I just have my activity show up. Any ideas?
The notification code:
Intent notificationIntent = new Intent(ctx, YourClass.class);
PendingIntent contentIntent = PendingIntent.getActivity(ctx,
YOUR_PI_REQ_CODE, notificationIntent,
PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) ctx
.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = ctx.getResources();
Notification.Builder builder = new Notification.Builder(ctx);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.some_img)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.some_big_img))
.setTicker(res.getString(R.string.your_ticker))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(res.getString(R.string.your_notif_title))
.setContentText(res.getString(R.string.your_notif_text));
Notification n = builder.build();
nm.notify(YOUR_NOTIF_ID, n);
You can't have a normal dialog without an activity. There are several possible workarounds though including styling the activity like a dialog and making the activity itself invisible and launching a dialog from it immediately.
You can probably use Remote Views to show a dialog without going to your app process.
you can set
Intent intent = new Intent(context, ReserveStatusActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
intent = new Intent(String.valueOf(PushActivity.class));
intent.putExtra("message", MESSAGE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(PushActivity.class);
stackBuilder.addNextIntent(intent);
// PendingIntent pendingIntent =
stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
0, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.BigPictureStyle notiStyle = new
NotificationCompat.BigPictureStyle();
android.app.Notification notification = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(en_title)
.setContentText(en_alert)
.setAutoCancel(true)
.setNumber(++numMessages)
.setStyle(notiStyle)
//.setContentIntent(resultPendingIntent)
.setContentIntent(pendingIntent)
.build();
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationManager.notify(1000, notification);
Related
I am building a notification which has ACCEPT and REJECT buttons but at the same time i also wants to open the activity when user touches over the notification. But in my case only both buttons working and when touched over the other parts than buttons it just disappears.
My efforts :
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
String NOTIFICATION_CHANNEL_ID = "BizPhone Channel";
int id = (int) System.currentTimeMillis();
Intent intent = new Intent(context, incoming_service.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra(Intent.EXTRA_PHONE_NUMBER, phoneNumber);
intent.putExtra(Intent.EXTRA_MIME_TYPES, callType);
intent.putExtra("NOTE", Integer.toString(id));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 1, intent, PendingIntent.FLAG_ONE_SHOT);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "BizPhone Notifications", NotificationManager.IMPORTANCE_HIGH);
// Configure the notification channel.
notificationChannel.setDescription("BizPhone related notifications");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
notificationManager.createNotificationChannel(notificationChannel);
}
// assuming your main activity
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setCategory(Notification.CATEGORY_CALL)
.setOngoing(true)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_call_black_24dp)
.setPriority(Notification.FLAG_INSISTENT)
.setContentTitle(phoneNumber)
.setContentText("Incoming Call")
.setSound(alarmSound)
.setContentIntent(pendingIntent)
.setContentInfo("Call");
Intent accept = new Intent(context, incoming_service.class);
accept.setAction(incoming_service.ANSWER);
accept.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
accept.putExtra(Intent.EXTRA_PHONE_NUMBER, phoneNumber);
accept.putExtra(Intent.EXTRA_MIME_TYPES, callType);
accept.putExtra("NOTE", Integer.toString(id));
PendingIntent pendingIntentYes = PendingIntent.getService(context, 10, accept, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.addAction(R.drawable.ic_permission_granted_24dp, "ACCEPT", pendingIntentYes);
Intent decline = new Intent(context, incoming_service.class);
decline.setAction(incoming_service.REJECT);
decline.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
decline.putExtra(Intent.EXTRA_PHONE_NUMBER, phoneNumber);
decline.putExtra(Intent.EXTRA_MIME_TYPES, callType);
decline.putExtra("NOTE", Integer.toString(id));
PendingIntent pendingIntentNo = PendingIntent.getService(context, 11, decline, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.addAction(R.drawable.ic_call_end_black_24dp, "REJECT", pendingIntentNo);
//notificationBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(id, notificationBuilder.build());
I have tried by looking at other threads too but it did not help. Any help would be appreciated and thanks in advance.
Change
setAutoCancel(true)
to
setAutoCancel(false)
Just try to add :
PendingIntent contentIntent = builder
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
I am answering my own question for future incoming users.
Why notification was disappearing on touch ( outside of 2 buttons but still on notification )
Because the base pendingintent was not a activity and it was a service. You can notice the code above. It should have been like :
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_USER_ACTION | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setClass(context, CallActivity.class);
intent.putExtra(Intent.EXTRA_PHONE_NUMBER, phoneNumber);
intent.putExtra(Intent.EXTRA_MIME_TYPES, callType);
And the 2 buttons code and their pendingintent which is service was okay.
Click on notification does not start a specific activity
My code
NotificationManager mNotifyMgr = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
int notificationId = (int) System.currentTimeMillis();
Intent picukUpIntent = new Intent(context, MainScreenActivity.class);
picukUpIntent.putExtra(MainScreenActivity.ORDER_ID, orderId);
picukUpIntent.putExtra(NOTI_TYPE, 3);
pendingIntent =
PendingIntent.getActivity(
context,
notificationId,
picukUpIntent,
PendingIntent.FLAG_ONE_SHOT
);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_mini_logo)
.setContentTitle("Title")
.setContentText(message)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true);
builder.setContentIntent(pendingIntent);
mNotifyMgr.notify(notificationId, builder.build());
As you can see, I try to open MainScreenActivity activity when the user clicks on the notification and it does not start MainScreenActivity when clicked. If I replace MainScreenActivity with other acitivies then it works just fine.
Note that I have tried many solutions.
add android:exported="true" in activity tag in manifest
change flag to PendingIntent.FLAG_CANCEL_CURRENT
If I try to start other activities then it works. The problem ony happens when I try to start specifically MainScreenActivity
Replace this code
pendingIntent = PendingIntent.getActivity(
context,
notificationId,
picukUpIntent,
PendingIntent.FLAG_ONE_SHOT);
to
pendingIntent = PendingIntent.getActivity(
context,
notificationId,
picukUpIntent,
PendingIntent.FLAG_UPDATE_CURRENT
| PendingIntent.FLAG_ONE_SHOT);
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
when i'm in my application,i click my application's notification open another my application,there are two same application on screen and btw i'm create notification from intent service
private void sendNotification(String msg, int msgdifferent) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("......")
.setContentText(msg).setContentInfo(msgdifferent + "");
Intent resultIntent = new Intent(this, MainActivity.class);
resultIntent.putExtra("Update", "YES");
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
You mean every notification open new activity. So i think you forgot to add this steps as referred from documentation
android:excludeFromRecents="true"
android:launchMode="singleTask"
android:taskAffinity=""
and also set your resultIntent flags like
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
Hi I want to show alert after click on my notification. Here is code:
#SuppressWarnings("deprecation")
private void Notify(String notificationTitle, String notificationMessage) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.icon_stat, "Powiadomionko", System.currentTimeMillis());
notification.ledARGB = Color.CYAN;//0xFFff0000;
notification.ledOnMS = 800;
notification.ledOffMS = 2400;
notification.vibrate = new long[]{100, 120, 100, 120};
Intent notificationIntent = new Intent(this, TerminarzActivity.class);
notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL | Notification.DEFAULT_VIBRATE;
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(Serwis_updateTERM.this, notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify(1, notification);
}
And I want to show Alert on anything not on my Activity after click. Anyone know how to do this ?
I know i must add
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
but nothing else..
Check out the creation of this notification (taken from Geofence sample). This code creates a notification and if you touch it it launches your MainActivity.
// Create an explicit content Intent that starts the main Activity
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
// Construct a task stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the main Activity to the task stack as the parent
stackBuilder.addParentStack(MainActivity.class);
// Push the content Intent onto the stack
stackBuilder.addNextIntent(notificationIntent);
// Get a PendingIntent containing the entire back stack
PendingIntent notificationPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Get a notification builder that's compatible with platform versions
// >= 4
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
// Set the notification contents
builder.setSmallIcon(R.drawable.ic_folder)
.setContentTitle(getString(R.string.geofence_transition_notification_title, transitionType, ids))
.setContentText(getString(R.string.geofence_transition_notification_text))
.setContentIntent(notificationPendingIntent);
// Get an instance of the Notification manager
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Issue the notification
mNotificationManager.notify(0, builder.build());
This code might help you, but still it depends what do you mean by "alert".