How to open current activity with android notification? - java

I would like to have a notification open the activity from which it is created. I am using ActionBarSherlock and my code looks like this:
public class CurrentActivity extends SherlockFragmentActivity {
private void remindMeAlert() {
Intent intent = new Intent(this, CurrentActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification noti = new NotificationCompat.Builder(this)
.setContentTitle("title")
.setContentText("text")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent).build();
noti.flags |= Notification.FLAG_AUTO_CANCEL;
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, noti);
}
}
This does not make any action when pressing the notification. However if I open any other activity like:
Intent intent = new Intent(this, DifferentActivity.class);
It works fine.

Related

Opening Different activities based on Notification action clicks

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

How resume app after click notification with Java Android?

I create a notification and I want to resume app after click notification. Now when I click notification , a notification dissapear , I want to resume my app (when is minimalize).
public static void getSynchronizeNotification(Context context, DataResponse dataResponse) {
Bitmap Largeicon = BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFICATION_SYNCHRONIZE_ID);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(), 0);
KeyguardManager km = (KeyguardManager) context.getSystemService(KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("IN");
kl.disableKeyguard();
PowerManager pm = (PowerManager) context.getSystemService(POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "SMOK Komunal");
wl.acquire();
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setCategory(Notification.CATEGORY_PROMO)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Zmiany po synchronizacji...")
.setStyle(new NotificationCompat.BigTextStyle().bigText(buildNotificationAfterSynchronizeText(dataResponse)))
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setLargeIcon(Largeicon)
.setContentIntent(pendingIntent)
.setVibrate(vibratePattern)
.setLights(Color.GREEN, 2000, 2000)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
notificationManager.notify(0, notificationBuilder.build());
// wl.release();
}
Edit this line
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
I do this and it works :
In MainAcitivity I have :
public static Context currentContext;
Next in all activity in onCreate and onResume and do this :
MainActivity.currentContext = this;
And next when I build a notification I do this :
Intent intent = new Intent(context, MainActivity.currentContext.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
This works;
<activity android:name=".YourActivity" android:launchMode="singleTop">

when i click notification open another my application

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);

Android How to show alert on another app

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".

Clearing Notification from invoked Activity

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);

Categories

Resources