question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

feat: Support for foreground "Pop on screen"/notification banner on Android

See original GitHub issue

Feature Request

Describe the Feature Request

Android has a feature called “Pop on screen” which allows an app to show a banner with a notification. This is especially handy when the app is open (foreground) and you want to trigger a Local Notification. However, in the Push Notification implementation of Capacitor, this is not supported (there is support for iOS). Please add support for Android’s “Pop on screen” feature as well.

capacitor-push-foreground

Platform Support Requested

  • Android
  • iOS
  • Electron
  • Web

Describe Preferred Solution

Ideally, the existing presentationOptions of Capacitor’s Push Notifications config should also be respected by the Android version.

Describe Alternatives

We tried https://github.com/katzer/cordova-plugin-local-notifications, but it suffers from the same issue.

Related Code

Additional Context

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:17
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

13reactions
jcarignancommented, Jun 22, 2020

Sorry for the late reply. I think the Ionic Team should add a note in their docs: PushNotifications received when the app is in the foreground don’t appear at all on Android.

Atleast you get the callback to send a LocalNotification instead, exactly how @Simbaclaws suggested.

@mccs0064 Here is my relevant code:

// There are 2 types of Firebase messages
// PUSH - containing atleast a title or a body, designed for user notifications
// DATA - NOT containing a title or body, designed for application updates

// When the app is in the background, this callback is only called for DATA messages

PushNotifications.addListener('pushNotificationReceived', notification => {    

    const isPushNotification = !!notification.title || !!notification.body;

    // if this is a push notification received when the app is in the foreground on Android
    if (isAndroid && isPushNotification) {

        // We schedule a LocalNotification 1 second later since Capacitor for Android doesn't show anything in this case
        LocalNotifications.schedule({
            notifications: [{
                title: notification.title,
                body: notification.body,
                id: new Date().getUTCMilliseconds(),
                schedule: {
                    at: new Date(Date.now() + 1000)
                },
                extra: notification.data,
                channelId: 'pop-notifications'
            }]
        });
    }
});
7reactions
jcarignancommented, May 24, 2020
if (isPlatform('android') {
    const notificationChannel: NotificationChannel = {
        id: 'pop-notifications',// id must match android/app/src/main/res/values/strings.xml's default_notification_channel_id
        name: 'Pop notifications',
        description: 'Pop notifications',                
        importance: 5,
        visibility: 1
    };

    PushNotifications.createChannel(notificationChannel);
    LocalNotifications.createChannel(notificationChannel);
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Notifications Overview | Android Developers
Appearances on a device · Status bar and notification drawer · Heads-up notification · Lock screen · App icon badge · Wear OS...
Read more >
Control notifications on Android - Google Support
Notifications. Under “Recently Sent,” tap an app. Tap a type of notification. Choose your options: Choose Alerting or Silent. To see a banner...
Read more >
Avoid notification popup peek when app is in foreground
Whenever I notify user, there will be a notification popup peek. ... popup peek, but only show a flashing icon in status bar....
Read more >
How to view your notification history on Android - The Verge
Select “Widgets” from the pop-up menu. Scroll down and long-press the “Settings” widget, then place it on your home screen. You'll get a...
Read more >
Firebase Notifications in Background & Foreground in Android
When the app is closed, your notifications are processed by the Google Service process, which take care of displaying your notifications as ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found