Skip to content

[FIREBASE_MESSAGING] <onBackgroundMessage not handling notification when app is on Background or Terminated> #2565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Yirmeyah-d opened this issue May 15, 2020 · 3 comments

Comments

@Yirmeyah-d
Copy link

Hi everyone,

I'm trying to get my onBackgroundMessage to execute when the app is on Background and a notification has been receive, but it doesn't execute myBackgroundMessageHandler.

I did everything that was written in the documentation at Optionally handle background messages, but it still doesn't work like i want,
When i receive a notification when the app is on Background, What i get is a notification without data (no app icon and no images, only title and body text).It's like onMessage was execute.

Here my code :
AndroidManifest.xml

<application
        android:name=".Application"

index.js

 message = {
            notification: { body },
            android: {
                notification: { click_action: 'FLUTTER_NOTIFICATION_CLICK',}
            },
            token: androidNotificationToken,
            data: {
              activityFeedItemId:activityFeedItemId,
              userReceivingNotificationId: userId,
              userActivatingNotificationPhotoUrl: activityFeedItem.userProfileImg,
              notificationType: activityFeedItem.type
            }
          };

buildgradle


dependencies {
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.firebase:firebase-analytics:17.2.2'
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.google.firebase:firebase-messaging:20.1.7'
}

apply plugin: 'com.google.gms.google-services'

MainActivity.java

package com.yimerah.ijn_amen;
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {
    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
    }
}

Application.java

package com.yimerah.ijn_amen;

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService;
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;

public class Application extends FlutterApplication implements PluginRegistrantCallback {
    @Override
    public void onCreate() {
        super.onCreate();
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    @Override
    public void registerWith(PluginRegistry registry) {
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }
}

OnBackgroundMessage


  onBackgroundMessage: myBackgroundMessageHandler
  myBackgroundMessageHandler{
    final FirebaseUser firebaseUser = await auth.currentUser();

    print("on message:$message\n");
    final String userReceivingId =
        message['data']['userReceivingNotificationId'];
    final String body = message['notification']['body'];
    final String notificationType = message['data']['notificationType'];
    final String notificationId = message['data']['activityFeedItemId'];
    if (userReceivingId == firebaseUser.uid) {
      int id = Uuid().parse(notificationId).reduce((a, b) => a + b);
        final String notificationMedia =
            message['data']['userActivatingNotificationPhotoUrl'];
        await showNotificationMediaStyle("", body, notificationMedia, id: id);
      

      print("Notification shown!");
    }
    print("Notification not shown!");
  }

Thank you in advance for your help,

@whatamelon
Copy link

Hi @Yirmerah, same problem as here.
So, I just try send data with notification.
But still don't work.
Is there any solution?

@TahaTesser
Copy link

duplicate of #2387

@Yirmeyah-d
Copy link
Author

@whatamelon https://stackoverflow.com/questions/61827613/firebase-messaging-onbackgroundmessage-not-handling-notification-when-app-is-on You should not send data with notification but only data or onBackgroundMessagee will not catch it.

@firebase firebase locked and limited conversation to collaborators Jul 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants