Android notification click, run the main intent? - android

Hy!
public static void addNotification(String sAppname, String sDescription) {
NotificationManager notifManager = (NotificationManager) mycontext.getSystemService(NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.icon, sDescription, System.currentTimeMillis());
Intent i = new Intent (mycontext, mStart.class)
.setAction(Intent.ACTION_MAIN)
.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(mycontext, 0, i, 0);
note.setLatestEventInfo(mycontext, sAppname, sDescription, contentIntent);
notifManager.notify(NOTIFY_ID, note);
}
mStart is my first activity
if i click on the notification this code is "re-create" the intent, and show up a new one.
i dont want this. i d like to show up the existing main intent. how to?

The solution is:
in manifest file add the following: singleTask="true"

Related

Resuming Android app from Notification

How do I resume an Android app from Notification? I want to be able to resume the instance from where it left, with all bounded Services, and settings etc. Instead of a complete new instance. This is my code:
String ns = Context.NOTIFICATION_SERVICE;
mNotificationManager = (NotificationManager)
getSystemService(ns);
Intent notificationIntent = new Intent(this, MeasurementsStarter.class);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, notificationIntent, 0);
// the next two lines initialize the Notification, using the configurations
// above
notification = new Notification(R.drawable.ic_launcher, "Metingen draaien",System.currentTimeMillis());
notification.setLatestEventInfo(getBaseContext(), "Metingen ManDown", "Metingen Mandown draaien nog",
contentIntent);
notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
I kind of found an answer, I manually set the data from all of my textFields etc. and just put my bindService in my onCreate() method. It's propably not the most graceous way, but it works nonetheless

How to check if Activity start from notification

I have problem my MainActivity can be created by 3 ways:
1) standart launch App
2) from Service
3) from notification click.
How I can check when it starts from notification click?
Notification code:
private void createNotification()
{
Log.d("service createNotification",MainActivity.TAG);
Context context = getApplicationContext();
Intent notificationIntent = new Intent(this,MainActivity.class);
intent.putExtra(AppNames.IS_NOTIFICATION_INTENT,true);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentTitle(this.getString(R.string.notification_title))
.setContentText(this.getString(R.string.notification_text))
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher);
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(AppNames.APP_NOTIFICATION, builder.getNotification());
}
add
intent.putExtra("started_from","notification");
to the code that starts the intent from the notifications, and the same thing to the other startActivity calls just change the value, then inside your activity
String startedFrom = getIntent().getStringExtra("started_from");
for more refer to this question: How do I get extra data from intent on Android?

Android: launch new activity when it not exist

I have a service that work in foreground, and when the Main activity launch it the notification is launched with the following code:
private void showNotification() {
Log.i(TAG, LocalService.class.getName() + "showNotification()");
Intent notificationIntent = new Intent(getApplicationContext(), NotificationActivity.class);
//notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(), 0, notificationIntent, 0);
nm = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
// Set the icon, scrolling text and timestamp
int icontouse = R.drawable.icon;
if (app.prefs.getBoolean("prefs_useprivacyicon", false)) {
icontouse = R.drawable.icon_privacy;
}
n = new Notification(icontouse, getString(R.string.voice_pro_ready), System.currentTimeMillis());
n.contentView = getRemoteView();
n.contentIntent = contentIntent;
startForeground(myID, n);
nm.notify(myID, n);
}
my problem is the PendingIntent, if the Main activity was in pause status, then this become in front when click on notify in statusbar, but if the Main activity was closed by finish() this don't become visible.
I need to make possible that if the Activity is in memory than become visible, if not available in memory, then launch a new instance.
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
changed to notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
and try it again

Click notification that takes you to a particular class

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

Android show notification from BroadcastReceiver

I have a class which extends BroadcastReceiver that gets called whenever new Wifi scan results are available (the receiver is registered in the manifest with the Scan_Results broadcast as the intent-filter).
From this class, I want to be able to show a notification to the user. Currently, I pass the context that is received as a parameter in the onReceive method of my broadcast intent class to a "show notification" method of another class.
When it gets to the line:
myNotificationManager.notify(notificationId, notification);
it fails with the following exception:
java.lang.IllegalArgumentException: contentView required: pkg=com.mumfordmedia.trackify id=2131034122 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x0)
Any idea why this is happening? All I can think of is because the context that I am getting from the onReceive parameter is not ... for lack of a better phrase, "right for the job"...
Any ideas?
Thanks,
Max.
ContentView is the view which is required when the notification is clicked. The code below works fine and setLatestEventInfo() is required method.
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,
"Hello from service", System.currentTimeMillis());
Intent intent = new Intent(this, MainActivity.class);
notification.setLatestEventInfo(this, "contentTitle", "contentText",
PendingIntent.getActivity(this, 1, intent, 0));
manager.notify(111, notification);
Not sure exactly why it wasnt working before but here is the code I got it working with:
Declare the following outside of any method:
int YOURAPP_NOTIFICATION_ID = 1234567890;
NotificationManager mNotificationManager;
Then in the onReceive method call the following:
mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
showNotification(context, R.drawable.icon, "short", false);
Then declare the following method:
private void showNotification(Context context, int statusBarIconID, String string, boolean showIconOnly) {
// This is who should be launched if the user selects our notification.
Intent contentIntent = new Intent();
// choose the ticker text
String tickerText = "ticket text";
Notification n = new Notification(R.drawable.icon, "ticker text", System.currentTimeMillis());
PendingIntent appIntent = PendingIntent.getActivity(context, 0, contentIntent, 0);
n.setLatestEventInfo(context, "1", "2", appIntent);
mNotificationManager.notify(YOURAPP_NOTIFICATION_ID, n);
}
You have to call Notification.setLatestEventInfo().
Use this code along with your notification
Intent intent = new Intent(this, MusicDroid.class);
PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(this, "This is the title",
"This is the text", activity);
notification.number += 1;
nm.notify(NOTIFY_ID, notification);
From what I can tell, when you're creating you notification to pass to the Notification manager, you're not giving it a content view to display. Review the line where you actually create the notification to see if you're actually giving the notification a view to display.
For those who are using NotificationCompat, the following code will work:
NotificationCompat.Builder n = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.icon).setContentText("Notify Title").setContentText("Sample Text");
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent i = new Intent(this,MainActivity.class);
PendingIntent ac = PendingIntent.getActivity(this, 0, i, 0);
n.setContentIntent(ac);
nm.notify(12222, n.build());

Categories

Resources