How to notify about the push notification when screen is locked? - android

Hello Everyone,
I am new to android development and currently working on google cloud messaging everything is working fine but the problem is that i am not able to get the notification icon and color defined by application on lock screen like other application gives like what's app green color and icon displayed on locked screen similarly gmail red color and icon displayed on home screen so can anyone tell me how to achieve it i am very thankful to you.
private void sendNotification(String msg) {
Log.d(TAG, "Preparing to send notification...: " + msg);
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg)
.setColor(
getResources().getColor(
R.color.abc_primary_text_material_dark))
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setContentInfo("hi")
.setTicker("hi")
.setPriority(Notification.PRIORITY_HIGH)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setLights(0x0000FF, 1000, 4000)
;
PowerManager pm = (PowerManager) getApplicationContext()
.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm
.newWakeLock(
(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
| PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP),
"TAG");
wakeLock.acquire(15000);
// Settings.System.putString(getApplicationContext().getContentResolver(),
// Settings.System.NEXT_ALARM_FORMATTED, "hi");
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Log.d(TAG, "Notification sent successfully.");
}

Notification notif = new Notification(
R.drawable.ic_launcher, message,
System.currentTimeMillis());
notif.flags = Notification.FLAG_AUTO_CANCEL;
notif.defaults |= Notification.DEFAULT_SOUND;
notif.defaults |= Notification.DEFAULT_VIBRATE;
Intent notificationIntent = new Intent(context, WelcomeActivity.class);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notif.setLatestEventInfo(context, title, message, contentIntent);

In your code, it's the icon set through setSmallIcon. The value R.drawable.ic_launcher in the code refers to an image in the res/drawable directory of your Android project. You can change it to R.drawable.<name of the icon you want to use>, as long as that image has been placed in the drawable folder.

Related

How to open app in notification in android?

In my app, when completing a task in async file, it shows a notification with this code
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pIntent = PendingIntent.getActivity(context.getApplicationContext(), 0, new Intent(), 0);
Notification noti = new Notification.Builder(context)
.setContentTitle("Complete")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.setAutoCancel(true)
.build();
notificationManager.notify(0, noti);
The problem is when I click the notification, nothing happens. Basically I want it so that, if the app is already open and the user clicks the notification, then nothing should open. If the app is not open (which means its still running but minimized) then I want it to open the app, like maximize it.
Does anyone know how to do this?
Thanks
You need an Intent, sample code:
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
This will open your MainActivity

<Android> Status bar notification does not get updated

I am sending a status bar notification in my application. Always appearing the 1st notification ,when i send my second(subsequent) notification it does not get updated and always show the first one. I am a new bee to Android , please help me to isolate the issue.
My code goes as below
//create notification manager
NotificationManager mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
//create notifcation
Notification notification = new Notification(R.drawable.ic_launcher,"My App", System.currentTimeMillis());
Intent intent1 = new Intent(this.getApplicationContext(),SaveTask.class);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this.getApplicationContext(), "New Msg","First message", pendingNotificationIntent);
mManager.notify(0, notification);
First of all you should start using Notification.Builder rather than directly create a new instance of Notification class
Here is what it should look using NotificationBuilder:
//create notification manager
NotificationManager mManager = (NotificationManager) this.getApplicationContext().getSystemService(this.getApplicationContext().NOTIFICATION_SERVICE);
Intent intent1 = new Intent(this.getApplicationContext(),MyActivity.class);
PendingIntent pendingNotificationIntent = PendingIntent.getActivity( this.getApplicationContext(),0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setTicker("My App")
.setContentIntent(pendingNotificationIntent)
.setContentTitle("New Msg")
.setContentText("First message")
.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mManager.notify(98038, notification);

android status bar notyfication icon

I'm trying to set an icon to the status bar, I can not view it as soon as you click on the button.
The problem there is the option to delete. I want to set up so you will not be deleted as long as those entering the application and delete
public void setNotificationToStatusBar(){
Intent intent= new Intent(this, PrefActivitySmsForwarder.class);
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0,intent, 0);
String forwarder_start_str= getResources().getString(R.string.sms_forwarding_activated);
String app_name=getResources().getString(R.string.app_name);
Notification n= new Notification(R.drawable.ic_launcher,forwarder_start_str, System.currentTimeMillis());
n.setLatestEventInfo(getApplicationContext(), app_name, forwarder_start_str, pi);
n.defaults= Notification.DEFAULT_ALL;
nm.notify(uniqueId, n);
finish();
}
String forwarder_start_str= getResources().getString(R.string.sms_forwarding_activated);
String app_name=getResources().getString(R.string.app_name);
Intent intent= new Intent(this, PrefActivitySmsForwarder.class);
Notification n= new Notification(R.drawable.ic_launcher,forwarder_start_str, System.currentTimeMillis());
/** I ADD THIS **/ n.flags=Notification.FLAG_ONGOING_EVENT;
PendingIntent pi=PendingIntent.getActivity(getApplicationContext(), 0,intent,0);
n.setLatestEventInfo(getApplicationContext(), app_name, forwarder_start_str, pi);
n.defaults= Notification.DEFAULT_ALL;
nm.notify(uniqueId, n);
finish();
This is how I create a notification. Using setOngoing(true) I make it persistent
http://developer.android.com/reference/android/app/Notification.Builder.html#setOngoing(boolean)
Intent intent = new Intent("MY_INTENT");
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(),0,intent,PendingIntent.FLAG_ONE_SHOT);
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("Title")
.setContentText("Click here")
.setSmallIcon(R.drawable.img2)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.img1))
.setContentIntent(pendingIntent)
.setOngoing(true)
.build();
If you want to keep your own way of creating the notification and not use the Notification.Builder, you could add this line of code to edit the flag field of the notification
notification.flags |= Notification.FLAG_ONGOING_EVENT;
you can do like this too
int icon = R.drawable.icon_notification;
String msg= "hey";
Intent notificationIntent = new Intent(context,Activity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(icon)
.setContentTitle(context.getString(R.string.app_name))
.setContentIntent(intent)
.setPriority(PRIORITY_LOW)
.setContentText(msg)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND |
Notification.DEFAULT_VIBRATE |
Notification.DEFAULT_LIGHTS);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(notificationId, mBuilder.build());

How can I set delete intent to my code?

I've searched many times but I was not able to find what I actually needed,
There is something on stackoverflow like this and this
but I need to implement my code for api level 8 not 11. So how can I implement it in my code?
My code is here:
....
mNotification = new Notification(icon, tickerText, when);
//create the content which is shown in the notification pulldown
mContentTitle = mContext.getString(R.string.noti_comes_t_l); //Full title of the notification in the pull down
CharSequence contentText = "click to see notification"; //Text of the notification in the pull down
//you have to set a PendingIntent on a notification to tell the system what you want it to do when the notification is selected
//I don't want to use this here so I'm just creating a blank one
mContentIntent = PendingIntent.getActivity(mContext, 0, new Intent(mContext, notify.class), 0);
//add the additional content and intent to the notification
mNotification.setLatestEventInfo(mContext, mContentTitle, contentText, mContentIntent);
//make this notification appear in the 'Ongoing events' section
mNotification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL ;
//show the notification
mNotificationManager.notify(NOTIFICATION_ID, mNotification);
....
How can I implement this?
I think you need NotificationCompat from the Support library, you can use this code to generate a notification, adapt it as you need
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(icon)
.setContentTitle(context.getString(R.string.app_name))
.setContentText(message)
.setDeleteIntent(PendingIntent);
mBuilder.setWhen(when);
Intent notificationIntent = new Intent(context, YOURCLASS.class);
notificationIntent.putExtra("url", url);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, random, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
mBuilder.setContentIntent(intent);
Notification notification = mBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(random, notification);

Android Notification Remote View Error

I have a notification that is shown. It works perfectly on my GS3 running Vanilla JB. When I try to run it on a Android 3.1 Emulator it crashes with the following exception. It is not a Custom Notification
12-28 00:26:05.239: E/AndroidRuntime(417): android.app.RemoteServiceException: Bad notification posted from package XX: Couldn't expand RemoteViews for: StatusBarNotification(package=XX id=0 tag=null notification=Notification(contentView=XX/0x1090087 vibrate=[500,500],sound=content://settings/system/notification_sound,defaults=0x0,flags=0x11) priority=0)
Here is my code:
public static void generateNotification(Context context, String message) {
// Show the notification
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Drawable largeIcon = context.getResources().getDrawable(R.drawable.ic_launcher);
Builder builder = new NotificationCompat.Builder(context);
builder.setContentTitle("XX");
builder.setContentText(message);
builder.setSmallIcon(R.drawable.ic_stat_push).setLargeIcon(drawableToBitmap(largeIcon));
builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
builder.setLights(Color.RED, 1000, 1000);
builder.setTicker("XX: " + message);
builder.setVibrate(new long[] { 500, 500});
Intent intent = new Intent(context, MainActivity.class);
intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("message", message);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentIntent(pendingIntent);
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
notificationManager.notify(0, notification);
}

Categories

Resources