Android statusbar code deprecated - android

Hi mates i'm trying to develop an app which needs to send a notification for the user at certain time. In the internet i found this code but eclipse says that it's deprecated.
Do you know which is the proper way to do it with my API? (16)
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
int icon = R.drawable.worldmap2;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText,when);
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
final Intent intent1 = new Intent(context, HttpExampleLimits.class);
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent1, 0);
notification.setLatestEventInfo(context, contentTitle,contentText, pIntent);
nm.notify(1, notification);
Thank you very much!

The best way to achieve it is to use Support library and NotificationCompat class.
You can find a lot of examples on offical doc: http://developer.android.com/guide/topics/ui/notifiers/notifications.html

Related

Migrate method setLatestEventInfo to API 23+

Currently I'm running with new environment in Android. Unfortunately, previous developer before I joined, it seems he using API 22. So, this is about legacy.
Now I have several issue in Notification, since setLatestEventInfo was removed (for 23+). I face with challenge to migrate past method. Here my code:
/depreciation/
#protected void PushNotification(String title, String message) {
String ns = Context.NOTIFICATION_SERVICE;
int NOTIFICATION_ID = 1;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = message;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = this;
CharSequence contentTitle = title;
CharSequence contentText = message;
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.defaults |= Notification.DEFAULT_SOUND;
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
Need suggest to do the right thing, several opinion suggest to adding NotificationCompat but I don't know how to do or any alternative for this?
Try use NotificationCompat.Builder

Android How to control the progressbar in a Notification (without Notification.Builder)?

For some reason, I don't get to use android-support-library in my code. In order to perform a Notification that matches Android 4.0 style, I write this:
private void noti() {
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int icon = R.drawable.ic_launcher;
CharSequence text = "Notification Text";
CharSequence contentTitle = "Notification Title";
CharSequence contentText = "Sample notification text.";
long when = System.currentTimeMillis();
Intent intent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
Notification notification = new Notification(icon,text,when);
notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent);
notificationManager.notify(1, notification);
}
How to control the progress bar in it? The way in developer.google.com is using Notification.Builder, but Builder is in the android-support-library.
I found another way to do this is
notification.setProgressBar(int viewId, int max, int progress, boolean indeterminate)
But another problem is that it needs a custom notification layout which contains a progress bar. If I use a custom layout, it won't match android 4.0 style.
What can I do now? Please help me, Thanks!
I solved the problem by using a custom layout witch came from android source code here , so it looks like a system Notification. Hope this can help others.

Android Status Bar Notification: make it un-clearable and have it return to app (not start a new instance)

I am developing an Android app and I want a status bar notification that cannot be cleared by the user. Does anyone know how to do that? I've seen them with apps like Skimble where a non-clearable notification appears while using the application.
Also, I'd when the user clicks/presses the notification, I want it to return to the app instance that is already running. Right now it starts a new instance. Again like the Skimble app, I just want to return to the already running instance of the app.
Thanks.
I found a solution to this problem. I used FLAG_ONGOING_EVENT to make the notification persist. Here's the code:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.mypic;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
notification.flags = Notification.FLAG_ONGOING_EVENT;
Context context = getApplicationContext();
CharSequence contentTitle = "Title Text";
CharSequence contentText = "Content text.";
Intent intent = new Intent(this, MyClass.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
Here is the solution for the NotificationCompat.Builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("MyApp")
.setContentText("MyMsg")
.setOngoing(true);
More at the Google docs.
notification.setLatestEventInfo is deprecated so you have to use the Notification.builder and if your using 2.X and lower API you have to use NotificationComacpt.Builder and this is not applicable and I couldn't make my notification stays. so I used setDeleteIntent and pointed to the same activity and on resume method just call the block of code which shows your notification. So it is kinda work around.

Android - notification manager, having a notification without an intent

I would like to be able to fire a notification to alert the users about a timer that has finished, however i do not wish to have an intent when you click the notification.
I've tried passing in null for the intent
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
int icon = R.drawable.icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
notification.setLatestEventInfo(context, contentTitle, contentText, null);
mNotificationManager.notify(HELLO_ID, notification);
You may pass the parameter
PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0)
instead of
null
on
notification.setLatestEventInfo(context, contentTitle, contentText, null);
The last parameter in setLatestEventInfo() is a PendingIntent and not an Intent. If you need the notification to not do anything when tapped is to pass an empty PendingIntent which is done as follows: PendingIntent.getActivity(context, 0, null, 0).
Interesting question and I would like to see if this works. I did a little digging and found a lot of ppl asking the same question. cbursk seems to have found a hack to get this intended functionality, which is to pass a Dialog to the notification intent instead of an Activity. I'm guessing the Dialog does nothing, or is programmed to dismiss itself right away, not sure. But I'm currently looking at this thread and going to test it out.

How to send 2 strings using Notification?

I'm trying to send 2 strings using Notification, but I don't know how should I do it. I used putExtra for my strings, but on the other activity I get null. I don't know what to do on the other activity. I read about this on net, but I didn't undersand. Can anyone help me?
Here is my code :
private void setNotifiy(){
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
CharSequence NotificationTicket = "You have a notification";
//CharSequence NotificationContent = ;
CharSequence contentTitle = "You are close to "+name_shop+"!!!";
CharSequence contentText ="So,you can go for shopping:)";
long when = System.currentTimeMillis();
Notification notification = new Notification(R.drawable.icon,
NotificationTicket, when);
Context context = getApplicationContext();
Intent notificationIntent = new Intent(this, ShopsOnMap.class);
notificationIntent.putExtra("latshop",lat_choose);
notificationIntent.putExtra("longshop", long_choose);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notificationManager.notify(NOTIFICATION_ID, notification);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
}
In the class ShopsOnMap I wrote just this :
String q = getIntent().getStringExtra("latshop");
System.out.println("!!"+q+"$$");
and I obtain q=null.
I need a simple example, or any idea that could help me. Thanks.
Use FLAG_UPDATE_CURRENT in your getActivity() call, instead of 0, for the fourth parameter, so your new Intent extras are applied to the PendingIntent.

Categories

Resources