Grouping Android Notifications - android

The below code is working fine with me in terms of creating the notification, with its voice and actions.
but looks the .setGroup(GROUP_KEY_Fonix) is not working, as the notifications are not grouped together, am I missing something here!
public int notificationID = 0;
final static String GROUP_KEY_Fonix = "fonix_notification";
private void Notify(String notificationTitle, String notificationMessage){
notificationID++;
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
// intent triggered, you can add other intent for other actions
Intent intent = new Intent(MainActivity.this, NotificationView.class);
PendingIntent pIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
// Build the notification, setting the group appropriately
Notification notif = new NotificationCompat.Builder(this)
.setGroup(GROUP_KEY_Fonix)
.setContentTitle("New note: " + notificationTitle)
.setContentText(notificationMessage)
.setSmallIcon(R.drawable.ic_stat_new_message)
.setContentIntent(pIntent)
.setSound(soundUri)
.addAction(R.drawable.ic_stat_new_message, "View", pIntent)
.addAction(0, "Remind", pIntent)
.build();
// Issue the notification
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
// hide the notification after its selected
notif.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(notificationID, notif);
}

Also, you should create group summary notification. For this just use this code:
Notification notificationSummary = new NotificationCompat.Builder(this)
//Set content
.setGroup(GROUP_KEY_Fonix)
.setGroupSummary(true)
.build();
notificationManager.notify(notificationSummaryId, notificationSummary);
Learn more here.

Related

How to send notifications depending on date

I have an calendar app where the info is downloaded online and there is a recyclerView of events and I want to notify the user when there is a calendar event for today, and notify with the event name or names. I have done some googling but haven't been able to figure this out.
In my MainActivity.java this is the method I created as a test from a page online
private void addNotification() {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setSmallIcon(R.drawable.ic_check);
mBuilder.setContentTitle("Notification Alert, Click Me!");
mBuilder.setContentText("Hi, This is Android Notification Detail!");
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
// Add as notification
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(0, mBuilder.build());
}
But when I call this method nothing is happening, what am I doing wrong?
I think you are missing the channel of the notification
I have a different solution, try this one (I checked it on my phone)
calling the function:
showSmallNotification(R.drawable.ic_launcher_background, "title","message");
Function code
private void showSmallNotification( int icon, String title, String message){
String CHANNEL_ID = "Channel_01";
String CHANNEL_NAME = "Notification";
// I removed one of the semi-colons in the next line of code
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
inboxStyle.addLine(message);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// I would suggest that you use IMPORTANCE_DEFAULT instead of IMPORTANCE_HIGH
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.enableVibration(true);
channel.setLightColor(Color.BLUE);
channel.enableLights(true);
//channel.canShowBadge();
// Did you mean to set the property to enable Show Badge?
channel.setShowBadge(true);
notificationManager.createNotificationChannel(channel);
}
Intent mainIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, mainIntent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), CHANNEL_ID)
.setVibrate(new long[]{0, 100})
.setPriority(Notification.PRIORITY_MAX)
.setLights(Color.BLUE, 3000, 3000)
.setAutoCancel(true)
.setContentTitle(title)
.setContentIntent(pendingIntent)
.setSmallIcon(R.mipmap.ic_launcher)
.setStyle(inboxStyle)
.setContentText(message);
// Don't forget to set the ChannelID!!
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
notificationBuilder.setChannelId(CHANNEL_ID);
}
notificationManager.notify(CHANNEL_ID, 1, notificationBuilder.build());
}
Edit
In order to check if there are any events every day you will need to use AlarmManager as from it's name you can schedule an event every day
You can see a solution (or at least it will give a direction) once you did that I think the next step would be to send the intent to a service which will check if there are any events available in current day. If there are any just show the notification/s
alarm manager link

Remove notification on clicking (Not a duplicate) [duplicate]

I just started working with notifications and now I'm trying to remove the notification and launch the app once the notification has been tapped in the notificationcenter.
I tried to work with the following code:
import android.app.NotificationManager;
public class ExpandNotification {
private int NOTIFICATION = 546;
private NotificationManager mNM;
public void onCreate() {
mNM.cancel(NOTIFICATION);
setContentView(R.layout.activity_on);
//Toast.makeText(this, "stopped service", Toast.LENGTH_SHORT).show();
}
I think this code executes the other class when tapped?
PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, new Intent(this, ExpandNotification.class), 0);
However the notification doesn't go away, nor does the application launch.
But I'm able to swipe it to left or right to remove it but that's not what I want..
To get the same effect using Notification.Builder or NotificationCompat.Builder call setAutoCancel(true) on the Builder instance.
Use the flag Notification.FLAG_AUTO_CANCEL
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
// Cancel the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
and to launch the app:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Create a new intent which will be fired if you click on the notification
Intent intent = new Intent(context, App.class);
// Attach the intent to a pending intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
This answer is too much late but specially i write following solution because notification constructor become deprecated so use notification using builder , like following :
**.setAutoCancel(true)** is used to remove notification on click
and entire notification is like follwoing :
private void makeNotification(String title,String msg){
Intent resultIntent = new Intent(this, MasterActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentIntent(resultPendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setAutoCancel(true)
.setContentText(msg);
int mNotificationId = 001;
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
Calling this method with title and message you get perfect notification.
You can directly add .setAutoCancel(true) this line into your code in order to remove notification on click.
This must be added in your builder variable.
Example:
mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("Notification")
.setContentText("Hello")
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
Best & simple way is set builder.setAutoCancel(true) it's cancel your notification after clicking on notification. I hope this code help you.
Builder for notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.btn_star);
builder.setContentTitle("This is title of notification");
builder.setContentText("This is a notification Text");
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
For open an activity on clicking notification
Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
Show bulder in notification
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(114, builder.build());
Complete code for show a notification with icon, image, title, description, auto cancel and on click open an activity
public void ShowIntentNotification(View v)
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.btn_star);
builder.setContentTitle("This is title of notification");
builder.setContentText("This is a notification Text");
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(114, builder.build());
}
For my it was that
.setPriority(Notification.PRIORITY_HIGH);
that was causing the notification to not clear after click... make sure you use:
.setPriority(Notification.PRIORITY_DEFAULT);
And .setAutoCancel(true) should work.

Cancel is not working in Activity to cancel a particular Push notification android

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.

Resume application by clicking notification from a service

I successfully created an activity which starts a service which by turn shows a notification like this
public int onStartCommand(Intent intent, int flags, int startId){
String notificationText = "working"
myNotification = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("test")
.setContentText(notificationText)
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
//FOLLOWING TECHNIQUE IS DEPRECATED
/* PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
myNotification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); */
myNotification.flags |= Notification.FLAG_NO_CLEAR;
myNotification.flags = Notification.FLAG_ONGOING_EVENT;
notificationManager.notify(1, myNotification);
return START_STICKY;
}
I know there are other questions related to this one BUT they all use setLatestEventInfo which is deprecated
And this is what I call from another activity:
protected void beginBackgroundSer(){
intentService = new Intent(this, Service.class);
intentService.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
this.startService(intentService);
}
So what I'd like is to resume the activity whenever I click the notification by non-deprecated methods. Thank you very much for your help.
You can create a notification by using the following steps.
Create a notification id for the future reference to update the notification by notification manager.
// Notification ID to allow for future updates
private static final int MY_NOTIFICATION_ID = 1;
Create the text elements for the notification.
// Notification Text Elements
private final CharSequence tickerText = "This is a Really, Really, Super Long Notification Message!";
private final CharSequence contentTitle = "Notification";
private final CharSequence contentText = "You've Been Notified!";
Define the notification Action elements i.e. when the user clicks on the view in the notification drawer what action happens? We use intents for handling this.
// Notification Action Elements
private Intent mNotificationIntent;
private PendingIntent mContentIntent;
You can also define sound and vibration for this notification.
// Notification Sound and Vibration on Arrival
private Uri soundURI = Uri
.parse("android.resource://com.examples.Notification.StatusBarWithCustomView/"
+ R.raw.alarm_rooster);
private long[] mVibratePattern = { 0, 200, 200, 300 };
Create the notification using the Notification.Builder class. Do this in your onCreate()
mNotificationIntent = new Intent(getApplicationContext(),
YourActivity.class);
mContentIntent = PendingIntent.getActivity(getApplicationContext(), 0,
mNotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
// Build the Notification
Notification.Builder notificationBuilder = new Notification.Builder(
getApplicationContext())
.setTicker(tickerText)
.setSmallIcon(android.R.drawable.stat_sys_warning)
.setAutoCancel(true)
.setContentIntent(mContentIntent)
.setSound(soundURI)
.setVibrate(mVibratePattern)
.setContent(mContentView);
// Pass the Notification to the NotificationManager:
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(MY_NOTIFICATION_ID,
notificationBuilder.build());
From the documentations
public NotificationCompat.Builder setContentIntent (PendingIntent intent) - Supply a PendingIntent to send when the notification is clicked. If you do not supply an intent, you can now add PendingIntents to individual views to be launched when clicked by calling RemoteViews.setOnClickPendingIntent(int,PendingIntent). Be sure to read Notification.contentIntent for how to correctly use this.
So you can use setContentIntent(PendingIntent intent) method something like
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, 0);
myNotification = new NotificationCompat.Builder(getApplicationContext())
.setContentTitle("test")
.setContentText(notificationText)
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(contentIntent) //added this new line
.build();
notificationManager.notify(1, myNotification);

Removing notification after click

I just started working with notifications and now I'm trying to remove the notification and launch the app once the notification has been tapped in the notificationcenter.
I tried to work with the following code:
import android.app.NotificationManager;
public class ExpandNotification {
private int NOTIFICATION = 546;
private NotificationManager mNM;
public void onCreate() {
mNM.cancel(NOTIFICATION);
setContentView(R.layout.activity_on);
//Toast.makeText(this, "stopped service", Toast.LENGTH_SHORT).show();
}
I think this code executes the other class when tapped?
PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, new Intent(this, ExpandNotification.class), 0);
However the notification doesn't go away, nor does the application launch.
But I'm able to swipe it to left or right to remove it but that's not what I want..
To get the same effect using Notification.Builder or NotificationCompat.Builder call setAutoCancel(true) on the Builder instance.
Use the flag Notification.FLAG_AUTO_CANCEL
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);
// Cancel the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
and to launch the app:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// Create a new intent which will be fired if you click on the notification
Intent intent = new Intent(context, App.class);
// Attach the intent to a pending intent
PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
This answer is too much late but specially i write following solution because notification constructor become deprecated so use notification using builder , like following :
**.setAutoCancel(true)** is used to remove notification on click
and entire notification is like follwoing :
private void makeNotification(String title,String msg){
Intent resultIntent = new Intent(this, MasterActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setContentIntent(resultPendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setAutoCancel(true)
.setContentText(msg);
int mNotificationId = 001;
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
Calling this method with title and message you get perfect notification.
You can directly add .setAutoCancel(true) this line into your code in order to remove notification on click.
This must be added in your builder variable.
Example:
mBuilder = new NotificationCompat.Builder(mContext);
mBuilder.setSmallIcon(R.mipmap.ic_launcher);
mBuilder.setContentTitle("Notification")
.setContentText("Hello")
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
Best & simple way is set builder.setAutoCancel(true) it's cancel your notification after clicking on notification. I hope this code help you.
Builder for notification
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.btn_star);
builder.setContentTitle("This is title of notification");
builder.setContentText("This is a notification Text");
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
For open an activity on clicking notification
Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
Show bulder in notification
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(114, builder.build());
Complete code for show a notification with icon, image, title, description, auto cancel and on click open an activity
public void ShowIntentNotification(View v)
{
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(android.R.drawable.btn_star);
builder.setContentTitle("This is title of notification");
builder.setContentText("This is a notification Text");
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
Intent intent = new Intent(Broadcastdemo.this, ThreadDemo.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 113,intent, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
manager.notify(114, builder.build());
}
For my it was that
.setPriority(Notification.PRIORITY_HIGH);
that was causing the notification to not clear after click... make sure you use:
.setPriority(Notification.PRIORITY_DEFAULT);
And .setAutoCancel(true) should work.

Categories

Resources