I am trying to display a notification about incoming call (from within my app) to a user. I found out that I can use NotificationCompat.Builder.setFullScreenIntent() to make a notification persistent on the top of the screen when application is running in the background.
However now when user does not answer the call and other party stops ringing how do I make that full screen intent disappear and show it inside a notification bar?
The only way I can think of is calling cancel() on that notification ID and creating a new one without full screen intent. But is this a good way of doing this? Is there a 'good practice' on how to achieve what I want?
Snippet showing how I create the notification:
public void displayNotification(String title, String text, int conversationId) {
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
this.createNotificationChannel();
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this.context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this.context, this.channelID);
builder.setContentTitle(title);
builder.setContentText(text);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setFullScreenIntent(pendingIntent, true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this.context);
notificationManager.notify(conversationId, builder.build());
}
Related
I have an notification that works fine yesterday, the notification not appears and I don't remember I had touched the code..
that notification must be appears when I get the desired value and must open a pop up dialog we user touch it.
can anyone help please?
the notification code - in side service-:
Intent notifyIntent = new Intent(context.getApplicationContext(), PopUp.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//how I tried to pass data
notifyIntent.putExtra("text1", text1);
notifyIntent.putExtra("text2", text2);
PendingIntent pIntent = PendingIntent.getActivity(context, 1, notifyIntent, 0);
// build notification
// the addAction re-use the same intent to keep the example short
Notification n = new NotificationCompat.Builder(context,CHANNEL_ID2)
.setContentTitle("check this")
.setSmallIcon(R.drawable.ic_delete)
.setContentIntent(pIntent)
.build();
NotificationManager notificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, n);
I'm using android studio emulator API 30
in the same service class I have a Notification and it is appearing to indicate that the service is working in the background, except the Notification that I but its code above it is not working suddenly
I don't know what is the problem, please help
When Issuing the notification with notify the notificationId must be a unique int for each notification. Update your code to include a random unique notificationId
see below
//you can use the timestamp
int notificationId = Integer.parseInt(String.valueOf(System.currentTimeMillis()));
NotificationManager notificationManager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// notificationId is a unique int for each notification that you must define
notificationManager.notify(notificationId, n);
Android Create a Notification
I want to turn On notification Access for my android app programitically.
In some android devices, Notification Access for my app is turned off by default. I want to turn On and turn Off the notification access for my app dynamically in the app itself.
But I don't have any idea on how to enable the service.
Please, provide me your views.
I don't think that this is possible. There are restrictions for android applications which you can pass only with root.
Use that kind of metod on your Activity
public void sendNotification() {
NotificationCompat.Builder builder = new NotificationCompat.Builder(getActivity());
builder.setSmallIcon(android.R.drawable.ic_dialog_alert);
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.journaldev.com/"));
PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0);
builder.setContentIntent(pendingIntent);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
builder.setContentTitle("Notification title");
builder.setContentText("Your notification message.");
builder.setSubText("link for more info.");
NotificationManager notificationManager = (NotificationManager) getActivity().getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
}
public void cancelNotification() {
String ns = NOTIFICATION_SERVICE;
NotificationManager nMgr = (NotificationManager) getActivity().getApplicationContext().getSystemService(ns);
nMgr.cancel(1);
}
You can prompt user to set those permissions for proper functioning. Setting those within app sounds malicious. I highly doubt such thing is possible. (Why would google create permission in first place if they can be overridden by app?)
I'm writing here because I'm facing a probleme that I could not resolve even after many researches and tries.
I'm currently developing an Android Library which consists only of java classes and fragment. The problem is I need to send Local Notifications to the user, and clicking on the notifications should send the user back to the activity where he was. At this point, my library sends the notifications just fine. But the click on the notification doesn't have any action.
In my notification reciver class (which extends the BroadcastReceiver class), when the notification appears, I create a Pending Intent but I don't know what I can give as parameters to send the user to the activity. I tried using intent filters but it give me no results
So how can I have the notification sending back the user to the application ? The best would be if I was able to have the notification sending back the user to the activity where the notification is created (but it's a fragment so...)
In an usual app, I would've an intent sending back the user to an activity class but my library needs to have only fragments.
Maybe there is no problem and the solution is easy since I'm new to notifications
If someone here have an idea thanks for helping me ! :D
And if my problem isn't clear (Because of my bad english as an example) don't hesitate to ask me to add informations ^^
**Edit from 29 April : **
I managed to achieve it by giving to my broadcast pending intent the canonical name of my class using :
mContext.getClass().getCanonicalName();
Once in my broadcast receiver class I just get the class from the name of the sending class :
Class<?> activityClass = null;
try {
activityClass = Class.forName(stringSourceClass);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Check out below code...
public BroadcastReceiver batteryReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
try {
String title = context.getString(R.string.app_name);
Intent intent1 = new Intent(context, YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),1,intent1,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(title)
.setContentText("Hello")
.setAutoCancel(false)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, notificationBuilder.build());
} catch (Exception e) {
}
}
};
check the Building a notification page:
Intent resultIntent = new Intent(this, ResultActivity.class);
...
// Because clicking the notification opens a new ("special") activity, there's
// no need to create an artificial back stack.
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
this,
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
just put your activity in resultIntent
how can I have the notification sending back the user to the
application ?
That's pretty simple:
1. While creating intent for pending intent call addAction ("action_name") method;
2. In activity you want to call (in manifest file) inside intent-filter tag add <action android:name="action_name>.
Now when your notification try to launch activity it would send intent message to system, which would search activity with proper action and launch it.
P.S. action name must be unique for every application
I have sync adapter that performs some operation in background. To notify my main activity about sync operation status, I used broadcast receivers, so my activity is able to receive messages from sync adapter. It works fine. However, I also need to display notification on android status bar, that indicates some sync results.
So I wrote simple method reponsible to disaply system notification:
private void sendNotification(Context ctx, String message)
{
Intent intent = new Intent(ctx, this.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent.FLAG_ONE_SHOT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Mobile Shopping")
.setContentText(message)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND | Notification.FLAG_SHOW_LIGHTS)
.setLights(0xff00ff00, 300, 100)
.setPriority(Notification.PRIORITY_DEFAULT);
//.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 , notificationBuilder.build());
}
Then, above method is called in onPerform sync:
#Override
public void onPerformSync(Account account, Bundle extras, String authority, ContentProviderClient provider, SyncResult syncResult)
{
.................
sendNotification(context, message);
}
Context is retrieved from constructor. It works without any problems, notification is showing.
However I also need to show main activity after user cicks on notification. So I believe I need to create PendingIntent and pass it to my notification builder (as it's commented in my code). But to pass main activity object to my sync adapter? Notification can be also displayed after auto sync finished.
Any tips?
So, I figured it out. Solution is to create pending intent this way:
Intent notificationIntent = new Intent(ctx, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
So after user clicks to notification, main activity will be shown.
I have looked at all the other AUTO-CANCEL-not-working questions here, and they all seem to involve mistakes that I am not making. I have tried both
builder.setAutoCancel(true);
and
Notification notif = builder.build();
notif.flags |= Notification.FLAG_AUTO_CANCEL;
Neither works.
I am using NotificationCompat since my minimum API is 8. Here is my full code. In this particular notification, I am not calling an intent, since I don't need the user to do anything.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setContentTitle(getString(R.string.app_name) + ": my title");
builder.setContentText(message);
builder.setSmallIcon(R.drawable.notification_icon);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.prog_icon);
builder.setLargeIcon(bitmap);
builder.setAutoCancel(true); // dismiss notification on user click
NotificationManager notiManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
notiManager.notify(MY_NOTI_MANAGER_ID, builder.build());
The notification displays just perfectly. You can swipe to clear it. But simply tapping it does not dismiss the notification. It just lights up and stay there.
Some possible differences between my code and others' posted here:
1) I am using NotificationCompat (which should not make a difference, but we've heard that before).
2) Since my notification is simple, I do not attach an intent.
Please let me know if you have any insights.
Edit: My purpose is to dismiss a notification without foregrounding my background app.
So apparently you do need a pending intent.
At Android - notification manager, having a notification without an intent, I found a solution that grabs the current active application as your pending intent (so that you don't have to start your own activity in order to dismiss the notification).
I just added the following two lines of code (right after setting the auto-cancel):
PendingIntent notifyPIntent =
PendingIntent.getActivity(getApplicationContext(), 0, new Intent(), 0);
builder.setContentIntent(notifyPIntent);
It worked great. I would say that if you don't want your activity to restart as a result of the user clicking your notification, then this is your best option.
You appear to be missing the PendingIntent and setContentIntent() call. I believe that is required for auto-cancel to work.
Here is some Notification-displaying logic from this sample project that works:
private void raiseNotification(Intent inbound, File output, Exception e) {
NotificationCompat.Builder b=new NotificationCompat.Builder(this);
b.setAutoCancel(true).setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis());
if (e == null) {
b.setContentTitle(getString(R.string.download_complete))
.setContentText(getString(R.string.fun))
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setTicker(getString(R.string.download_complete));
Intent outbound=new Intent(Intent.ACTION_VIEW);
outbound.setDataAndType(Uri.fromFile(output), inbound.getType());
b.setContentIntent(PendingIntent.getActivity(this, 0, outbound, 0));
}
else {
b.setContentTitle(getString(R.string.exception))
.setContentText(e.getMessage())
.setSmallIcon(android.R.drawable.stat_notify_error)
.setTicker(getString(R.string.exception));
}
NotificationManager mgr=
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mgr.notify(NOTIFY_ID, b.build());
}
Hai dear friend if you want to show non cancelable
notification(not cancelable for users) for a particular
time and after that you need clear it (like the music player) you can use this.
mNotificationBuilder .setSmallIcon(android.R.drawable.btn_plus);
mNotificationBuilder .setContentTitle("My notification");
mNotificationBuilder .setContentText("Notificattion From service");
mNotificationBuilder .setLights(0xFF0000FF, 500, 500);
Notification note = mNotificationBuilder.build();
note.flags = Notification.FLAG_ONGOING_EVENT; // For Non cancellable notification
mNotificationManager.notify(NOTIFICATION_ID, note);