I have a alarm manager which is calling an activity class named ScheduleAlert.
public class ScheduleAlert extends ActivityGroup {
private String notificationAlart, editEventid;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...........
..........
// ************* Notification ************//
NotificationManager mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final Notification notifyDetails = new Notification(R.drawable.icon, "Myapp", nextAlarmTime);
Context context = getApplicationContext();
CharSequence contentTitle = "Myapp";
CharSequence contentText = notificationAlart;
Intent notifyIntent = new Intent(context, MyApp.class);
PendingIntent pendingIntent = PendingIntent.getActivity(ScheduleAlert.this, 0, notifyIntent,android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP);
notifyDetails.setLatestEventInfo(context, contentTitle, contentText,pendingIntent);
notifyDetails.flags = Notification.FLAG_FOREGROUND_SERVICE | Notification.FLAG_AUTO_CANCEL;
notifyDetails.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;
mNotificationManager.notify((int) editEventid, notifyDetails);
// ************* Notification ************//
this.finish();
}
public void onDestroy(){
super.onDestroy();
}
}
I want that the indent of MyApp activity should fire when I tap on the notification massage. At the time of notification I want just sound and vibration. But now the I am getting the sound and vibration, and also the MyApp activity is fired, which I do not want actually. What is problem in my code?
There are lots and lots of strange things with this code:
I have no idea why you are extending ActivityGroup for this code
Do not use getApplicationContext() in most circumstances, such as this one
Since this is an ActivityGroup (for whatever reason) and not a Service, it is misleading to the OS and the user to have FLAG_FOREGROUND_SERVICE
FLAG_FOREGROUND_SERVICE and FLAG_AUTO_CANCEL make little sense in combination
However, I would not expect any of this to cause MyApp to automatically start. In fact, AFAIK, there is no circumstance in which a Notification will automatically invoke its PendingIntent without the user tapping on it. I suspect that your real problem lies elsewhere.
Related
i'm beginner in android.i create a service to show a notification when onCreat on mainActivity called
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent a = new Intent(this,myService.class);
startService(a);
}
}
here is my service class
public class myService extends Service{
private NotificationManager mManager;
#Override
public void onCreate() {
mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MainActivity.class);
Notification notification = new Notification(R.drawable.ic_launcher,"This is a test message!", System.currentTimeMillis());
intent1.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP| Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1,PendingIntent.FLAG_UPDATE_CURRENT);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(this.getApplicationContext(), "AlarmManagerDemo", "This is a test message!", pendingNotificationIntent);
mManager.notify(0, notification);
}
but i want if my app is not running i can show this service for example if my phone time is bigger than 9 i show this notification only one time in a day.any ideas ? thanks in advance
Notifications are running on background and application running is not needed. While application is not running, notifications also work and this is the main mission of them. I suggest you Vogella's tutorial and documentation:
http://www.vogella.com/tutorials/AndroidNotifications/article.html
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
You don't want to show notifications while your app is running. That is just annoying and has (usually) no point. There are of course exceptions.
If you want to show a notification at a specific time, you should consider to use the AlarmManager.
If you want to trigger the notification from a server or wherever else, use Google Cloud Messaging.
I have a GCMIntentService class, in which i get some messages returned from my server. I want to be able to start a certain activity when a certain message arrives at my app. For example if in my onMessage() method (onMethod() is the method, and the first place, in the app, that receives the messages from the server) arrives the string = "tomatoe", i want to start a specific activity. The way i know right now to start an activity, is this:
Intent resactivity = new Intent(getApplicationContext(), ResponseActivity.class);
resactivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(resactivity);
The problem is that GCMIntentService, is not a class that extends activity, and i believe that i can't use this code for this purpose. Is there some way to initiate an activity from a class that is not an activity, by creating an intent inside that class?
The problem is that GCMIntentService, is not a class that extends activity, and i believe that i can't use this code for this purpose.
GCMIntentService inherits from Context, which is where startActivity() is defined.
Bear in mind, though, that your users may attack you with pitchforks or machine guns for popping up activities at random points in time, perhaps in the middle of something else that they are doing. Please make this behavior configurable, or else make very very certain that your users will appreciate these interruptions.
I post the method I usually use to start an activity when a notification arrives in my apps. Take a look to all the configurations and remove those you are not interested in:
#Override
protected void onMessage(Context context, Intent intent) {
String app_name = context.getString(R.string.app_name);
String message = intent.getStringExtra("payload");
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager notificationManager = (NotificationManager) context.getSystemService(ns);
int icono = R.drawable.ic_stat_notify;
long time = System.currentTimeMillis();
Notification notification = new Notification(icono, app_name, time);
notification.defaults |= Notification.DEFAULT_SOUND;
Intent notificationIntent = new Intent(context, ResponseActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, -1, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.when = System.currentTimeMillis();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, app_name, message, pendingIntent);
notificationManager.notify(0, notification);
}
You should be able to do it as such:
getApplication().startActivity(resactivity);
I'm working on an application to display the battery status of then phone/tablet. I have it running and working as it is, but somehow the application is 'killed' when the screen is turned off.
The strange thing is though that is is killed on my LG phone, but not on my Galaxy Tab when the screen is off/locked.
The source code is as following:
public class BatteryLevelActivity extends Activity {
private TextView batterLevel;
CharSequence tickerText = "No need to charge at the moment";
public void onCreate(Bundle SavedInstanceState) {
super.onCreate(SavedInstanceState);
setContentView(R.layout.main);
this.registerReceiver(this.mBatInfoReceiver, new IntentFilter(
Intent.ACTION_BATTERY_CHANGED));
batterLevel = (TextView) findViewById(R.id.batteryLevel);
};
private void notif(int level) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "BatteryWatch";
CharSequence contentText = "Remaining charge level is " + level + "%";
Intent notificationIntent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
}
private BroadcastReceiver mBatInfoReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context arg0, Intent intent) {
int level = intent.getIntExtra("level", 0);
batterLevel.setText("Battery level is at " + level + "%\n\n"
+ tickerText);
notif(level);
tickerText = "";
}
};
}
Somehow I think that I should have a method that is called something like 'onScreenOff' or maybe 'onScreenOn' and then the application could be reloaded, but I'm open to suggestions as to how to keeo it running when the screen is off/locked.
Thanks in advance
You probably want to consider using a Service because you want this running long term, the developer guide here explains more. Also (although I cannot find the link) want to tell Android that it should restart your app if it has to kill it. I cannot remember exactly what this is called
** Additional Info **
I gave slightly false information in the above. I believe you can make a Service re-start after Android has killed it but NOT an Activity. In the link above for the developer guide for Service it's detailed there. It's called starting a service as sticky, see the sub-section Extending the Service Class.
I am sending notification depending on some logic.
public class DbAdapter_Assignment extends DbAdapter {
public DbAdapter_Assignment(Context context) {
super(context);
}
public void deleteUnassignedTask(Assignment[] assignments)
{
//some logic
sendNotification(String a. String b);
}
private void triggerNotification(String s, String id)
{
CharSequence title = "TASK UNASSIGNED";
CharSequence message = s;
NotificationManager notificationManager;
notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.checkno, s, System.currentTimeMillis());
notification.flags = Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
// No. 1 solution
PendingIntent pendingIntent = PendingIntent.getActivity(context, Integer.parseInt(id), null, PendingIntent.FLAG_UPDATE_CURRENT);
// -----------
// No. 2 solution
Intent notificationIntent = new Intent(context, MyTask.class);
notificationIntent.putExtra("EmpID", Functions.getLoginEmpID(context));
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP );
PendingIntent pendingIntent = PendingIntent.getActivity(context,
Integer.parseInt(id), notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
// ----------------
notification.setLatestEventInfo(context, title, message, pendingIntent);
notificationManager.notify(Integer.parseInt(id), notification);
}
}
No. 1 solution is working. the thing I want to add is if click on that notification, go to MyTask class. So I tried No. 2 solution and IT"S NOT WORKING for me. It doesn't show any notification if do no. 2 solution. What am I missing? Is ir because of the class is not an activity or servise ?
Your Notification looks right to me.
I haven't tried to create a Notification outside of a class that extends Activity, but I would assume if you can access the NotificationManager then you should be able to do it.
Does the class you're pointing the PendingIntent to extend Activity?
Could there be a problem in the Activity that your PendingIntent is using?
Is that Activity in the AndroidManifest.xml?
I am new to android development and I try to create a background download feature for my app. I followed this http://developer.android.com/guide/topics/ui/notifiers/notifications.html#CustomExpandedView to create my custom notification.
The downloading is performed, I checked the downloaded file in the sdcard. Also,the status bar icon and title are changed properly.
The problem is that the custom layout I provide for the notification does not appear (expand under the bar). Here is the related code parts inside private AsyncTask class:
#Override
protected void onPreExecute() {
// create and configure the notification
notification = new Notification(R.drawable.download, "Downloading map..", System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
//create a custom layout for the notification
myContentView = new RemoteViews(appContext.getPackageName(), R.layout.download_progress);
myContentView.setImageViewResource(R.id.status_icon, R.drawable.ic_menu_save);
myContentView.setTextViewText(R.id.status_text, "download in progress");
myContentView.setProgressBar(R.id.status_progress, 100, 0, false);
notification.contentView = myContentView;
notification.contentView.apply(appContext, dl.getListView());
//instantiate the pending intent
Intent myIntent = new Intent(appContext, DownloadList.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
int requestID = (int) System.currentTimeMillis();
PendingIntent myPendingIntent = PendingIntent.getActivity(appContext, requestID, myIntent, 0);
notification.contentIntent = myPendingIntent;
//add the Notification object to the notification manager
notificationManager = (NotificationManager) appContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIF_ID, notification);
}
#Override
protected void onProgressUpdate(Integer... progress) {
//update progress bar
notification.contentView.setProgressBar(R.id.status_progress, 100, progress[0], false);
notificationManager.notify(NOTIF_ID, notification);
}
}
Note that my DownloadList class extends ListActivity.
Do I need to do something more that just "notification.contentView = myContentView;" in order to inflate the layout?
Hmm... Well I compared your code to my code that already works... and I don't see many differences... But, it is possible that one of these minor differences is important.
final Notification notification = new Notification(R.drawable.icon, "Downloading", System.currentTimeMillis());
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
notification.contentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.download_progress);
notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ic_status);
notification.contentView.setTextViewText(R.id.status_text, "Downloading in progress");
notification.contentView.setProgressBar(R.id.status_progress, 100, progress, false);
Intent notificationIntent = new Intent(MainPage.mainActivity, MainPage.class);
PendingIntent contentIntent = PendingIntent.getActivity(MainPage.mainActivity, 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
//getApplicationContext();
final NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(
Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_MESSAGE, notification);
First, I looked at your old code and noticed that the NOTIF_ID = 1 I'm not so sure that is a good idea because what if someone else has an ID of one. Of course I could be mistaken about that, but I just pounded in a number like 792489743 and I expect no one else would have the same number. Just a precaution I suppose.
Second, I didn't get to see if the resources were correct? What does the stack trace say? I suppose that it would've just quit out on it if there was a problem there though.
Third, I put my in its own task as Service kinda as follows
public class DownloadService extends IntentService {
//initializing code and stuff
private class DownloadTask extends AsyncTask<String, Void, Boolean> {
and I did it in the doInBackground This way if the user kills the app or what not it wouldn't kill the download.
Lastly, I've never used apply I don't personally see how it would hurt, but I haven't seen an example that uses it either.
Hope this helps some!
It was an emulator problem after all.....
It lagged when I "dragged down" the notification! I killed some CPU extensive processes on my PC resulting to a faster emulator.
Lesson learned. Leave the heavy multitasking to pros or to another PC.