The following is the function responsible for handling a push notification received by my app. It is supposed to redirect the user to a certain Activity in the app after he/she clicks clicks on the notification.
When I try running the app on my Android 2.3.6(API 10) device,the notifications are handled perfectly.
However,when I try running it on an AVD( Andriod 4.3 (API 18)),there is no effect- the default android logo is displayed as icon instead of ic_launcher.png, the user is not redirected to the app on click,the notification does not autohide on clicking.
Please note,that I am using if statements to check API version of the phone on which the app is installed so as to ensure that push notifications are not affected by deprecated classes.
#SuppressWarnings("deprecation")
#SuppressLint("NewApi")
public static void createNotification(Context context, String payload) {
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 11) {
Intent intent = new Intent(context, tabswipe.MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,intent, 0);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification n = new Notification.Builder(context)
.setContentTitle("New mail from ggg")
.setContentText("zzzz")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.addAction(R.drawable.ic_launcher, "Call", pendingIntent)
.addAction(R.drawable.ic_launcher, "More", pendingIntent)
.build();
notificationManager.notify(0, n);
}
if (currentapiVersion < 11) {
Intent intent = new Intent(context, tabswipe.MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,intent, 0);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher,"New Update from VIIT Perception", System.currentTimeMillis());
notification.flags |= Notification.FLAG_AUTO_CANCEL; // Hide the notification after its selected
notification.defaults |= Notification.DEFAULT_LIGHTS; //adding LED lights to notification
notification.setLatestEventInfo(context, "Updates from VIIT Perception", payload, pendingIntent);
notificationManager.notify(0, notification);
}
}
Related
I send a simple notification to the user when they open the app. When I swipe the notification away from the status bar, the notification is also deleted.
I just want the notification to stay on the status bar even if I close the app like whats app Instagram and some other apps.
This is how I send notification
Context context = getActivity();
Intent intent1 = new Intent(context,MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent1, 0);
Notification.Builder builder = new Notification.Builder(context)
.setTicker("Notification")
.setContentTitle("Important Message")
.setContentText("This is an example of a push notification using a Navigation Manager")
.setSmallIcon(R.drawable.ic_add)
.setContentIntent(pIntent);
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
//These are necessary for the notification to pop up
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.O){
builder.setPriority(Notification.PRIORITY_MAX);
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);
builder.setLights(Color.BLUE, 500, 500);
}
//after android O we must use notification channels
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
String channelId = "Your_channel_id";
NotificationChannel channel = new NotificationChannel(
channelId,
"Reminder to remind to review your notes",
NotificationManager.IMPORTANCE_HIGH);
channel.setDescription("Hello Dear friends"); //this is to test what this is
notificationManager.createNotificationChannel(channel);
builder.setChannelId(channelId);
}
Notification notification = builder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
P.S1: I don't want the notification to persist like in foreground services. I just want to not go away when the app is swiped away, but the user can get rid of the notification anytime they want.
P.S2: I am not using any services, just a simple notification. Like e.g.letting the user know that they collected some badges today.
Here I'm showing a push notifications in android fromService class which is showing fine and getting dismiss if we tap on complete Remoteview but when I'm opening an Activity from buttons/texts of RemoteView, notifications don't get cancelled.
In Service:
Intent notificationRecordFollowupintent = new Intent(this, RecordFollowUpActivity.class);
notificationRecordFollowupintent.putExtra("notify_id", m);
PendingIntent contentRecordFollowupIntent = PendingIntent.getActivity(this, m, notificationRecordFollowupintent, PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
builder.setSmallIcon(R.mipmap.ic_launcher)
.setWhen(System.currentTimeMillis())
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.setStyle(bigTextStyle)
.setCustomBigContentView(contentView)
.setCustomContentView(contentView)
.setGroup("Follow Up Alert")
.setNumber(++numMessages)
.setOngoing(true)
.setAutoCancel(true)
.setContentIntent(contentRecordFollowupIntent);
} else {
builder.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Follow Up Alert")
.setContentText("" + name)
.setWhen(System.currentTimeMillis())
.setDefaults(NotificationCompat.DEFAULT_SOUND)
.setStyle(bigTextStyle)
.setCustomContentView(contentView)
.setCustomBigContentView(contentView)
.setContentIntent(contentRecordFollowupIntent)
.setAutoCancel(true);
}
Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
nm.notify(getResources().getString(R.string.app_name), m, notification);
In Activity:
Intent intent = getIntent();
int notificationId = intent.getIntExtra("notify_id", -1);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notificationId);
I'm getting same Id also still it doesn't get cancelled.
NotificationManager.cancelAll() to remove all notification. NotificationManager.cancel(notificationId) to remove particular notification.
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);
I wanted to create something like an instant messaging application. How do I display multiple messages all in one notification? I can create a notification that appear when user receive a single notification. But when the user receive more than one message how can I update the notification with the previous message? Should I save the messages into a database and display it out if the user did not cancel the notification? Or is there any other way that I can handle this?
Below is my notification code.
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, "IMTest- A new event is created" , when);
Intent notificationIntent = new Intent(context, IM_Chat.class);
notificationIntent.putExtra("topicId", topicId);
notificationIntent.putExtra("sender", sender);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(context, 1, notificationIntent, Intent.FLAG_ACTIVITY_MULTIPLE_TASK | PendingIntent.FLAG_CANCEL_CURRENT);
notification.setLatestEventInfo(context, topicName, "A new event ["+eventName+"] is added in "+topicName, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.ledARGB |= 0xff0000ff;
notification.ledOffMS |= 1000;
notification.ledOnMS |= 300;
notificationManager.notify(CommunitiesappConstant.NOTIFICATION_ID, notification);
You can update notification using same id and builder
http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Updating
private NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
private mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
//Different Id's will show up as different notifications
private int mNotificationId = 1;
//Some things we only have to set the first time.
private boolean firstTime = true;
private updateNotification(String message, int progress) {
if (firstTime) {
mBuilder.setSmallIcon(R.drawable.icon)
.setContentTitle("My Notification")
.setOnlyAlertOnce(true);
firstTime = false;
}
mBuilder.setContentText(message)
.setProgress(100, progress, true);
mNotificationManager.notify(mNotificationId, mBuilder.build());
}
I am creating a small app for android for notification.
But it gives an Error in Notification class error (API level supported for 11 or 16).
Then I tried using NotificationCompat class but it shows resources can not be resolved to a type while I import package import android.support.v4.app.NotificationCompat.Builder;
In other words, if I use Notification class then it give API level Error, and if I use NotificationCompat then it gives that resource error. How can I resolve both these errors?
public void createNotification(View view) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Context context=new Context();
// Build notification
// Actions are just fake
NotificationCompat.Builder noti = new NotificationCompat.Builder(this)
.setContentTitle("New mail from " + "star.ankit90#gmail.com")
.setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.addAction(R.drawable.ic_launcher, "Call", pIntent)
.addAction(R.drawable.ic_launcher, "More", pIntent)
.addAction(R.drawable.ic_launcher, "And more", pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
I've had compilation errors using your code.
This fixed code compiles successfully:
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Build notification
// Actions are just fake
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle("New mail from " + "star.ankit90#gmail.com")
.setContentText("Subject").setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pIntent)
.addAction(R.drawable.ic_launcher, "Call", pIntent)
.addAction(R.drawable.ic_launcher, "More", pIntent)
.addAction(R.drawable.ic_launcher, "And more", pIntent).build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Hide the notification after its selected
Notification notification = builder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
Maybe it will work for you. if not a stack trace can help.