ionic-team / capacitor-plugins

Official plugins for Capacitor ⚡️
518 stars 583 forks source link

Bug: Local notifications doesn't popup in Android 12 when app is in background #745

Closed ndabAP closed 2 years ago

ndabAP commented 2 years ago

Bug Report

Plugin(s)

Capacitor Version

💊   Capacitor Doctor  💊 

Latest Dependencies:

  @capacitor/cli: 3.3.3
  @capacitor/core: 3.3.3
  @capacitor/android: 3.3.3
  @capacitor/ios: 3.3.3

Installed Dependencies:

  @capacitor/ios: not installed
  @capacitor/cli: 3.3.3
  @capacitor/core: 3.3.3
  @capacitor/android: 3.3.3

[success] Android looking great! 👌

Platform(s)

Current Behavior

A local notification doesn't popup when the app is in background. It only shows as a white dot in the top bar.

Screenshot_20211218-221312

Expected Behavior

Show the local notification in the same way as when the app is in foreground.

Screenshot_20211218-221340

Code Reproduction

Started from scratch.

const channel: Channel = {
  id: "pop-notifications",
  name: "channel",
  importance: 4, // Tried also 5
  visibility: 1,
};

PushNotifications.createChannel(channel);
LocalNotifications.createChannel(channel);

PushNotifications.addListener(
  "pushNotificationReceived",
  async (notification) => {
    await LocalNotifications.schedule({
      notifications: [
        {
          id: Date.now(),
          title: notification.title as string,
          body: notification.body as string,
          channelId: "pop-notifications",
          schedule: {
            at: new Date(Date.now() + 1000),
          },
        },
      ],
    });
  }
);

Other Technical Details

Additional Context

jcesarmobile commented 2 years ago

Are you sure pushNotificationReceived is being called? It looks like what you see is the local notification shown by the OS, not the local notification you create inside pushNotificationReceived and it's using the default channel, which has an importance of 3.

You can assign the channel from the own notification payload with android_channel_id, see docs, or if using firebase console, there is a field for the channel too. How does your push notification payload looks like?

I can't reproduce if I create a local notification to a channel with importance of 4, schedule it for later and background the app.

ndabAP commented 2 years ago

You can assign the channel from the own notification payload with android_channel_id, see docs, or if using firebase console, there is a field for the channel too.

Thank you very much. I needed many days to figure that out ;) But now I found out that I need to set my channel ID in Firebase as well.

ionitron-bot[bot] commented 2 years ago

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of the plugin, please create a new issue and ensure the template is fully filled out.