Notifications in Android when the phone is sleeping - android

I have been using Notifications in an app I am building. The way to build notification has changed a lot since older versions it seems, but in the end I managed to display a notification. (*)
I have two problems though:
1) The notification makes no sound. It is not a matter of increasing the volume of notifications in the tablet because notifications from gmail for example do produce sound. How do I make a notification that produce sound and vibration? (I am trying this in Android 6)
2) Even more important, how do I make a notification that "notifies" me even when the phone is sleeping? So far my notifications do happen when the phone is sleeping but I have to turn on the phone to see them. Ideally these should appear even when the screen is black
EDIT: just to be clear, the notifications are happening when the phone is sleeping. It is just that I don't see them unless I turn on the screen, then they are. I would like for the notification to "turn on the screen"
(*) My current code is
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent pi = PendingIntent.getActivity(context, 0, new Intent(), 0);
Notification notification= new Notification.Builder(context)
.setContentTitle(message)
.setContentText("Proximity Alert!")
.setSmallIcon(R.mipmap.ic_launcher)
.setAutoCancel(true)
.setContentIntent(pi).build(); //getNotification();
//notification.flags= Notification.FLAG_AUTO_CANCEL;
//notification.setLatestEventInfo(context, "GAST", "Proximity Alert", pi); //this is deprecated
notificationManager.notify(NOTIFICATION_ID, notification);

Yes, you can add sound while sending notification with this way.
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICAT‌​ION);
builder.setSound(defaultSoundUri);
And to do it while sleeping phone you will have to use service.
Documentation : Services

Related

Android push disappears after a few seconds

My test phone is a Samsung Galaxy S6 using Android 7.0 Nougat.
In other apps, Android push notifications stay in the status bar, but in my app the push notification disappears after the notification is received.
Problem is DISAPPEAR NOTIFICATION FROM STATUS BAR.
Not about NOTIFICATION ALARM.
Push notification is well done working.
After ringing, there is no push in status bar.
Does anyone know about this situation?
Here is the code:
NotificationCompat.Builder builder = new
NotificationCompat.Builder(MainActivity.this)
.setSmallIcon(R.drawable.icon_noti)
.setContentTitle('My App')
.setContentText('test')
.setDefaults(Notification.DEFAULT_SOUND)
.setPriority(Notification.PRIORITY_HIGH)
.setAutoCancel(true);
PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this,
0, new Intent(getApplicationContext(), MainActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(contentIntent);
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, builder.build());
If someone knows about this issue, please answer.
Thanks for reading.
For me it works perfectly (except for the single quotes here .setContentTitle('My App') and here .setContentText('test'))!
Maybe there are some system issues - or maybe this behavior is caused by some other part of your app.
By the way, note that NotificationCompat.Builder is deprecated; now it requires an additional String channelId.
I had this issue and the problem was that I had two different notifications with the same id.
One was tied to a service so when I unbound the service it killed the notification but since both had the same ID it killed both notifications.
Changing the notification id when calling the notify method fixed the issue:
notificationManager?.Notify(2, notification); //Previously I had "1" which matched another notification that had 1 as ID
Took me like 4 hours to find out but at the end in my scenario it had nothing to do with the notification settings.

Text Messaging Android Application

How do smart watches receive text messages from mobile phone? I am looking to make a similar application and I am not sure where to start. I want to make an application for my tablet that will show text message notification.
To create a notification that is transmitted to connected Android Wear devices, you need to use the NotificationCompat class located in the Support v4-Package.
The detailed documentation how to do this can be found here.
The code essentially breaks down to this code snippet (copied from the documentation):
int notificationId = 001;
// Build intent for notification content
Intent viewIntent = new Intent(this, ViewEventActivity.class);
viewIntent.putExtra(EXTRA_EVENT_ID, eventId);
PendingIntent viewPendingIntent =
PendingIntent.getActivity(this, 0, viewIntent, 0);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_event)
.setContentTitle(eventTitle)
.setContentText(eventLocation)
.setContentIntent(viewPendingIntent);
// Get an instance of the NotificationManager service
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
// Build the notification and issues it with notification manager.
notificationManager.notify(notificationId, notificationBuilder.build());
This creates a notification on the handheld as well as connected Android Wear devices, without even creating a Wear-App. The pairing and transmitting process is handled by the OS.
As a general hint, the Training section of Android Developers is always a good place to start researching.

Different text for the wear device and the mobile device

Is it possible to have a notification that shows a different text (content title and content text) in the Android wear device and in the mobile device?
Not at the moment. However, you can achieve this effect in the following way:
post a notification on the phone with setLocalOnly(true)
post a DataItem using a DataAPI that describes the notification and changed text
when the wearable receives the DataItem, post the notification with different text, again setting setLocalOnly(true)
on each notification alse call setDeleteIntent so you know, when there are dismissed
when on of the notifications gets dismissed, delte the DataItem from point 2.
when the DataItem gets deleted, you will receive a callback; delete the remaining notification
There might be some corner cases here I don't see immediately, but the general approach should allow you to achieve what you want.
Yes, it's possible now with little tricky and bug on Android wear which help's us. More Over this trick doesn't need Android wear API, just a normal Notification with RemoteViews is enough.
NotificationCompat.Builder mBuilder;
mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentText("This msg won't display in your phone, only on wear you can see.")
.setContentTitle("Hello")
.setContentIntent(
PendingIntent.getActivity(context, 10,intent,PendingIntent.FLAG_ONE_SHOT));
NotificationManager mNM = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = mBuilder.build();
RemoteViews contentView = new RemoteViews(context.getPackageName(),
R.layout.notification_layout);
contentView.setTextViewText(R.id.noti_text,"This message won't display in your wear device, only on phone you can see.");
contentView.setImageViewResource(R.id.noti_image,R.drawable.ic_launcher);
notification.contentView = contentView;
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNM.notify(50, notification);
Now run the app on your device and check the notification on both watch and phone, the content inside the RemoteViews won't display in your watch but on phone it will display and if you remove the .setContentTitle() & .setContentText(), then the RemoteViews content will be displayed on both watch and phone too.
Actually you can achieve this using
.setGroup(GROUP_KEY)
.setGroupSummary(true)
On your phone notification. Then create a notification with the data that you want to show on the watch and set
.setGroup(GROUP_KEY)
to the same group that your phone notification. This is used to show stack notifications but if you use it with only one then it does the trick.
Complete documentation: Stacking Notifications

Android Wear Notification is not displayed if FLAG_NO_CLEAR is used

If flag "FLAG_NO_CLEAR" is used the notification gets not displayed on the Android Wear.
Does anyone know why or any workaround? I didn't find any information in the documentation.
I need the flag "FLAG_NO_CLEAR" on my notifications and have Action button for "dismiss", "snooze" etc.!
Notification flag FLAG_NO_CLEAR basically makes your notification "ongoing". Ongoing notifications posted from phone will NOT be displayed on the wearable device.
You have two solutions to your problem - both of them have advantages and disadvantages. Please read text below and decide which solution will solve your situation better:)
Solution 1 - use group:
You can make use of group feature of Android Wear framework. It's basically created to post many (grouped) notifications on wearable device and one summary notification on phone. But using this mechanism you can also post one ongoing notification on your phone and second notification only on wear. You will end up with one ongoing notification on your phone and one normal notification on your wearable device.
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// This notification will be shown only on phone
final NotificationCompat.Builder phoneNotificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title phone")
.setContentText("Text phone")
.setOngoing(true)
.setOnlyAlertOnce(true)
.setGroup("GROUP")
.setGroupSummary(true);
// This notification will be shown only on watch
final NotificationCompat.Builder wearableNotificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title wearable")
.setContentText("Text wearable")
.setOngoing(false)
.setOnlyAlertOnce(true)
.setGroup("GROUP")
.setGroupSummary(false);
notificationManager.notify(0, phoneNotificationBuilder.build());
notificationManager.notify(1, wearableNotificationBuilder.build());
This method will allow you to post an "alternative" notification for watch only, keeping your ongoing notification on phone. But (as mentioned earlier) the notification on watch cannot be ongoing - it has to be a normal notification. If you really want to have a true ongoing notification on watch you'll need to go for second solution.
Please read more about grouping (stacking) notifications here:
https://developer.android.com/training/wearables/notifications/stacks.html
Solution 2 - create wearable app:
Ongoing notifications from phone won't be shown on watch, but you can create wearable part of your Android application and post your notification directly on Android Wear. You can easily post an ongoing notification from there, but it won't be the same notification as is on phone. You will need to sync them between both devices.
Please read more about DataApi here:
https://developer.android.com/training/wearables/data-layer/index.html
https://developer.android.com/training/wearables/data-layer/data-items.html
You can also take a look at my post where I've posted a code demonstrating how to use a DataApi in practise:
https://stackoverflow.com/a/24896043/3827276
Honestly something really weird changed in wear 1.5
so a solution would be to set an alarm
Uri alarmSound = getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Intent displayIntent = new Intent(this, WearNotif.class);
//displayIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent displayPendingIntent = PendingIntent.getActivity(this,
0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("WeirdShit")
.setContentText("some random crap")
.extend(new Notification.WearableExtender()
.setDisplayIntent(displayPendingIntent))
.setSound(alarmSound)
.build();
NotificationManagerCompat notificationManager =
NotificationManagerCompat.from(this);
notificationManager.notify(25454, notification);

How to check which notifications are active in status bar in Android Dev?

I have made an app that sets notifications in the drop-down status bar of Android phones. However, there is a bug in my code (sometimes the notifications are set, sometimes they are not). I want to be able TO CHECK (in the code) IF THE NOTIFICATION IS VISIBLE TO THE USER. (i.e. can the user see the notification in the status bar?).
How can I do this? (Thanks in advance).
Sample code is greatly appreciated.
I want to be able TO CHECK (in the code) IF THE NOTIFICATION IS VISIBLE TO THE USER. (i.e. can the user see
the notification in the status bar?).
How can I do this?
You can't, sorry. Update: Now possible with Android 4.3+ http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()
However, you can always simply cancel() it -- canceling a Notification that is not on-screen is perfectly fine. Conversely, you can always safely call notify() again for the same Notification, and it too will not cause a problem if the Notification is already on-screen.
EDIT:
NotificationManager.getActiveNotifications() was added in API 23 if you don't want to use the NotificationListenerService
Just to put all together. This is how it works
To build a notification,
Notification n = new Notification.Builder(MyService.this)
.setContentTitle("Notification Title")
.setContentText("Notification Message")
.setSmallIcon(R.drawable.myicon).build();
To make a notification sound call setSound() of Notification,
Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Notification n = new Notification.Builder(MyService.this)
.setContentTitle("Notification Title")
.setContentText("Notification Message")
.setSound(alarmSound)
.setSmallIcon(R.drawable.myicon).build();
To cancel the notification after user selected and launched the receiver Intent, call setAutoCancel(),
Notification n = new Notification.Builder(MyService.this)
.setContentTitle("Notification Title")
.setContentText("Notification Message")
.setSound(alarmSound)
.setAutoCancel(true)
.setSmallIcon(R.drawable.myicon).build();
To make sound/vibrate only once for a particular notification use Notification.FLAG_ONLY_ALERT_ONCE. With this flag, your notification will make sound only once till it gets cancelled and you can call notify() as many times as you want with the notification id. Note that if you call cancel() or if user cancelled the notification or auto cancelled, notify() call will make the notification sound again.
n.flags |= Notification.FLAG_ONLY_ALERT_ONCE; // Dont vibrate or make notification sound
Finally to put the notification on notification panel,
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(notification_id, n);
Note that notification_id here is important if you want to use the notification effectively.( to keep single sound/vibration for a notification or to cancel a specific notification).
To cancel a particular notification,
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancel(notification_id);
You can cancel() a notification even if it doesn't exist or you can call notify() as many times as you want with the same id. Note that calling notify with different id will create new notifications.
So, regardless of whether the notification exist or not, if you call notify() again with the correct notification_id with the Notification.FLAG_ONLY_ALERT_ONCE flag set, you can keep your notification alive without disturbing the user with repeated sounds.
You need to set an id for each notification you make.
so you make a notification ..
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notId + selectedPosition, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis() - offset, pendingIntent);
Notification notification = new Notification(R.drawable.icon, "TVGuide Υπενθύμιση", System.currentTimeMillis());
NotificationManager manger = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
notification.setLatestEventInfo(context, "Κανάλι: " + b.getString("channel"), "Εκπομπή: " + showname, pendingIntent);
manger.notify(notId, notification);
to clear it..
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,notId, intent, 0);
pendingIntent.cancel();
and to check if active..( existAlarm returns null if no pending intent available)
public PendingIntent existAlarm(int id) {
Intent intent = new Intent(this, alarmreceiver.class);
intent.setAction(Intent.ACTION_VIEW);
PendingIntent test = PendingIntent.getBroadcast(this, id + selectedPosition, intent, PendingIntent.FLAG_NO_CREATE);
return test;
}
So everything comes down to initialize an ID for each notification and how you make it unique.
A new method is introduced to the NotificationManager class in API 23:
public StatusBarNotification[] getActiveNotifications()
There exists a flag for that.
Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
FLAG_ONLY_ALERT_ONCE:
...should be set if you want the sound and/or vibration play each time the notification is sent, even if it has not been canceled before that.
Although, the notification will blink when it is sent again, but there won't be any sound or vibration.
It's possible now to check notifications outstanding in android 4.3 upwards
See here:
http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()
It seems that from Android M (API 23) it is possible to get your process like that, without using NotificationListenerService nor requiring additional permissions:
notificationManager.getActiveNotifications()
As of Android Marshmallow (API 23), you can recover a list of active notifications posted by your app. This NotificationManager method is getActiveNotifications(). More info here: https://developer.android.com/reference/android/app/NotificationManager.html#getActiveNotifications()

Categories

Resources