11

EDIT: I discovered these are known as FLOATING NOTIFICATIONS. Anyone knows how to enable them by default from the app (through a permission etc) on an Android device?

I am currently testing push notifications on an Android device and noticed that although I get the push notifications, they are not popped up on the screen but stay in the background (I need to drag down the top status bar as per picture). I see this as rather useless as the user is not really notified if he's using the phone:

enter image description here

On iPhone, the pop up displays correctly with no issues whatsoever.

Community
  • 1
  • 1
James
  • 3,597
  • 11
  • 41
  • 76

2 Answers2

6

I've finally made it work using Expo.Notifications.createChannelAsync using priority: "max" and when sending a message add channelId: "mychannel".

Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
IC_
  • 1,624
  • 1
  • 23
  • 57
  • Hi, i have set it as below: Notifications.createChannelAndroidAsync('JOBCONFIRMATION', { name: 'JOBCONFIRMATION', `sound: true, vibrate: [0, 250, 250, 250], priority: 'max', }); However, it work well on expo app, but when i expo:build it;s not working anymore.. i go to the notification settings and check those settings in default is set as silent. Do u experience this after build as standalone apps? – Benson Toh Jan 15 '19 at 03:26
  • Setting `priority` to `"max"` doesn't work, valid values it says are only "normal" "default" or "high". – Noitidart Oct 06 '19 at 16:27
  • @Noitidart according to https://docs.expo.io/versions/latest/sdk/notifications/ `max` is a valid option for priority. – Delali May 13 '20 at 17:42
  • @assetCorp I just tested it I got the error: `error: Error occured while expo.sendPushNotificationsAsync, error: Error: "value" must be an object, "value" at position 0 fails because [child "priority" fails because ["priority" must be one of [default, normal, high]]].` – Noitidart May 13 '20 at 22:07
  • 1
    `priority (optional) (min | low | default | high | max) -- Android may present notifications in this channel differently according to the priority. For example, a high priority notification will likely to be shown as a heads-up notification. Note that the Android OS gives no guarantees about the user-facing behavior these abstractions produce -- for example, on many devices, there is no noticeable difference between high and max.` @Noitidart this is an excerpt from the expo docs found at https://docs.expo.io/versions/latest/sdk/notifications/. What version of expo are you using? – Delali May 14 '20 at 12:24
  • Thanks @assetCorp I'm on 35 - ill try updating and then try this to see if it works. Will update you. But shouldn't this not affect version of expo, because I made a call to their server? And the server responded with that error. – Noitidart May 14 '20 at 13:17
  • did not found this method in "SDK 39" – Sushil Dec 01 '20 at 08:23
0

To show a so called floating notification, app must have the permission SYSTEM_ALERT_WINDOW.

A permission is something given by the user. So, there is no way to enable them by default from the app. What you can do is to take user to the settings page.




Check if you already have the permission

The recommended way to do this is using Settings#canDrawOverlays(Context).This permission is introduced only in API level 23. Therefore as per doc, it there is no need to check the permission in older devices.

However it is seen that this won't work well as expected in some pre-Marshmallow devices like Xiaomi Mi 2. So is better to do it as shown in this post.

Take user to the settings page if there is no permission

If the app targets API level 23 or higher, the app user must explicitly grant this permission to the app through a permission management screen. The app requests the user's approval by sending an intent with action Settings.ACTION_MANAGE_OVERLAY_PERMISSION.

Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.setData(Uri.parse("package:" + context.getPackageName()));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
context.startActivity(intent);
Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
  • 1
    Ironically that's the exact mobile I have, and which I have been using for testing! – James Oct 01 '18 at 08:55
  • If this is true how are Yahoo Mail, Fave, Grab and a tonne of other apps on my Android able to display FLOATING NOTIFICATIONS from push messages? I have never granted these apps any express permissions from the permissions management screen. It simply must be possible to implement what they have done. – eddyoc May 17 '19 at 20:18