firebase / quickstart-android

Firebase Quickstart Samples for Android
https://firebase.google.com
Apache License 2.0
8.82k stars 7.32k forks source link

quickstart-android messaging app doesn't receive data messages in background #89

Closed 2ndGAB closed 8 years ago

2ndGAB commented 8 years ago

Step 1: Are you in the right place?

But I cannot get it working on Nexus 7 LTE 6.0.1 or Samsung GT-9295 5.0.1 as well.

Step 3: Describe the problem:

I send the following message with Advanced REST Client, the message is sent successfully, but is only received in foreground and not when the application is in background.

{
    "to" : "egu9jGiMcew:APA91bFv2RewdzlrC4Q0 ... dYZgv91oZVfTtrfAojLMKZZmEURvbq-aA2",
    "data": {
        "title": "Title test",
        "msg": "Text of the test",
        "code": 2
    },
    "delay_while_idle" : false,
    "priority" : "high",
    "content_available" : true
}

Steps to reproduce:

  1. Build the quickstart-android project
  2. Execute the messaging app
  3. Send it a data message when the application is in background

    Observed Results:

    • In my case, the message given above is not received. Is there any mistake in it?
    • It seems that the message is received by the device as I see the following messages:
    07-18 15:28:34.371 3068-3068/? W/GCM-DMM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000000 pkg=com.google.firebase.quickstart.fcm (has extras) }

    Expected Results:

    • The message should be received and generate a notification the application is in foreground or in background.

      Relevant Code:

My FirebaseMessagingService is:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";

    /**
     * Called when message is received.
     *
     * @param remoteMessage Object representing the message received from Firebase Cloud Messaging.
     */
    // [START receive_message]
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        // [START_EXCLUDE]
        // There are two types of messages data messages and notification messages. Data messages are handled
        // here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
        // traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
        // is in the foreground. When the app is in the background an automatically generated notification is displayed.
        // When the user taps on the notification they are returned to the app. Messages containing both notification
        // and data payloads are treated as notification messages. The Firebase console always sends notification
        // messages. For more see: https://firebase.google.com/docs/cloud-messaging/concept-options
        // [END_EXCLUDE]

        // TODO(developer): Handle FCM messages here.
        // Not getting messages here? See why this may be: https://goo.gl/39bRNJ
        Log.i(TAG, "From: " + remoteMessage.getFrom());
        Log.i(TAG, "Notification Message Body: " + remoteMessage.getData());

        sendNotification("FCM message received");

        // Also if you intend on generating your own notifications as a result of a received FCM
        // message, here is where that should be initiated. See sendNotification method below.
    }
    // [END receive_message]

    /**
     * Create and show a simple notification containing the received FCM message.
     *
     * @param messageBody FCM message body received.
     */
    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_stat_ic_notification)
                .setContentTitle("FCM Message")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}
miroslavign commented 7 years ago

I can confirm the same issue, using this configuration: devices: Nexus 5 AND Google Pixel, Firebase dependencies: compile 'com.google.firebase:firebase-core:10.0.1' compile 'com.google.firebase:firebase-messaging:10.0.1' IDE: Android studio 2.2, stable channel

Log: Server sends data-only message, but if the app is killed, I get: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000000 pkg=com.whatever (has extras) }

Judging by @2ndGAB I should try newer AS release ?

miroslavign commented 7 years ago

Hmmm, with this Firebase, each day you learn something extraordinary :)

Updating to latest Android Studio(2.3 beta 1) and accompanying gradle version (as well as buildToolsVersion '25.0.0'), it seem to work as intended, that is, seem to work even if app is in "swipe to kill" or in background. Will keep tracking this, as a number of customers using our application filed this issue. I'll push a new version to the store and will inform you if this hack really works for a greater device pool, so we all can close this one and get some sleep.

2ndGAB commented 7 years ago

Hello @miroslavign, Glad to here that, I arrived to the same conclusion where this feature strangely stopped to work with the first 2.X AS versions.

miroslavign commented 7 years ago

New issue, will test it more to determine, but we had a complaint from two customers using latest N version (Nexus 6P and Pixel). We will investigate more if this is actually version specific now.

2ndGAB commented 7 years ago

@miroslavign, @kroikie in fact I recently discovered that sometimes, at least because I'm not absolutely sure it's not something new again, data messages are not received when we install a debug apk!!

miroslavign commented 7 years ago

@2ndGAB tnx for sharing that. I'm gonna concentrate on testing release builds only, but then, I'm not sure if we're going to be able to see the message: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000000 pkg=com.whatever (has extras) }

miroslavign commented 7 years ago

@kroikie a question related to making a service go to a new process using android:process=":remote"

After inserting that line, I get a crash on my service (and it is not running after that): java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.whatever:remote. Make sure to call FirebaseApp.initializeApp(Context) first.

Now, before this, I did not use anything in my background service related to FCM, so, why and (more importantly) how could I do this in my service ? Initialize what ? Adding this my service, does not work out of the box: @Override public void onCreate() { FirebaseApp.initializeApp(this);

Any ideas, because, putting my service (which should be always running no matter if app is killed), may resolve most of the problems our app has, related to FCM missing documentation...

alcastelo commented 7 years ago

Anyone is having some news about this issue?

I find what seems the same problem.

Android 4.4, firebase:firebase-messaging:10.0.1, AS:2.3 beta 3 We made a little App with an activity and a couple of services. First service is the FCM receptor, second One is a service that we will keep working in foreground with his own notification.

When the activity is running, if the FCM device gets a message, the onMessageReceived is launched as intended. But if we swipe out the Activity, and the foreground service is still working, when a message is received, the service simply dissapears with a Force stopping message. Same behavior in android 5 and 5.1, in android 6 and 7 seems to work as intended.

2ndGAB commented 7 years ago

@alcastelo Just to verify something, did you have a look at at some articles upper in this thread related to the Android Studio version if you use it?

geraldoandradee commented 7 years ago

Same issue here:

`05-31 15:33:34.493: W/GCM(3435): Mismatched messenger

05-31 15:34:52.593: W/GCM-DMM(3435): broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000000 pkg=com.whatever (has extras) }

05-31 15:34:52.603: W/GCM-DMM(3435): Unregister application com.whatever for user 0`

I'm using AS 2.3.2, JRE: 1.8.0_112., Firebase 10.2.4, device is Samsung Galaxy J5 (SM-J500M) and Android version is 6.0.1

flachica commented 7 years ago

Same issue here https://github.com/phonegap/phonegap-plugin-push/issues/707#issuecomment-309211376:

06-17 13:51:52.028 4952 7647 I SendBroadcastPermission: action:com.google.android.c2dm.intent.RECEIVE, mPermissionType:0 06-17 13:51:52.042 4952 4952 W GCM-DMM : broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=info.larinconadasisepuede.lrssp (has extras) }

I'm using AS 2.3.3 recently updated, device Huawei P8, Android 6.0, EMUI 4.0.1

rahuldevanavar91 commented 7 years ago

@2ndGAB I am using AS 2.3.3 compile SDK version -25 Support lib version - 25.3.1 Google Play services version -11.0.4

When app is in foreground I get notification with data, but when an app is in the background or killed notification not received with data,

I get the following logs W/GCM-DMM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=packgeName (has extras) }

it works perfectly with notification but I can't customize. Tested on One plus 5 .OS 7.1.1

finally here is my answer.https://stackoverflow.com/a/45810771/1993001

loeschg commented 7 years ago

Have also seen this on a beta Crashlytics build, and I'm positive I had not manually pressed FORCE STOP in app settings. I'm able to repro by going into app settings and force closing the app.

Swiping to close the app does not cause this issue (at least not in my tests).

08-11 11:19:30.900 ? W/GCM-DMM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000000 pkg=com.my.package (has extras) }
Florin-Birgu commented 7 years ago

@loeschg what phone model do you use?

loeschg commented 7 years ago

@Florin-Birgu Pixel (7.1.2)

I wish I could provide more info, but I'm not able to reproduce the scenario. I did have it happen periodically (~1x a week for a couple months). I haven't seen it for a few weeks now.

Random bits of info that may help correlate a root cause:

Edit: Also realizing this may not be the proper ticket for this discussion. Arrived here from Google-ing. Let me know if I should open a separate ticket somewhere.

aka-awasthi commented 7 years ago

same thing happening with me ,bothering me from week now this message came when my app is swiped from tray

GCM-DMM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.lifepage.jfh (has extras) }

and so far reading the suggestions above the only solution i got is install android studio 1.5 is this correct or there are other solutions for this problem???

device - oneplus 3t android studio - 2.3.3 build version - 25.0.3

i tried fcm and gcm both.

log with gcm --

08-14 10:20:58.677 8904-8923/com.lifepage.jfh D/FA: Logging event (FE): session_start(_s), Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=TutorialActivity, firebase_screen_id(_si)=-4275405240345914240}] 08-14 10:20:58.699 8904-8923/com.lifepage.jfh V/FA: Using measurement service 08-14 10:20:58.699 8904-8923/com.lifepage.jfh V/FA: Connecting to remote service 08-14 10:20:58.712 8904-8923/com.lifepage.jfh D/FA: Connected to remote service 08-14 10:20:58.712 8904-8923/com.lifepage.jfh V/FA: Processing queued up service tasks: 1 08-14 10:20:58.774 19291-9018/? V/FA-SVC: Event recorded: Event{appId='com.lifepage.jfh', name='session_start(_s)', params=Bundle[{firebase_event_origin(_o)=auto, firebase_screen_class(_sc)=TutorialActivity, firebase_screen_id(_si)=-4275405240345914240}]} 08-14 10:21:03.747 8904-8923/com.lifepage.jfh V/FA: Inactivity, disconnecting from the service 08-14 10:22:32.461 8904-10577/com.lifepage.jfh D/GcmNotificationListener: From: 213539449012 08-14 10:22:32.461 8904-10577/com.lifepage.jfh D/GcmNotificationListener: Message: 20170518-0001 08-14 10:22:32.462 8904-10577/com.lifepage.jfh D/GcmNotificationListener: data payload accepted 08-14 10:22:48.557 3486-3486/? D/SystemServicesProxy: getRecentTasks: com.lifepage.jfh 08-14 10:22:48.602 2779-2856/? D/StatusBarManagerService: manageDisableList userId=0 what=0x0 pkg=Window{c7c64b5 u0 com.lifepage.jfh/com.lifepage.jfh.TutorialActivity} token=android.os.Binder@98727f9 which=1 08-14 10:22:48.607 8904-10802/com.lifepage.jfh D/AppTracker: App Event: stop 08-14 10:22:48.610 8904-10803/com.lifepage.jfh V/FA: Recording user engagement, ms: 119948 08-14 10:22:48.611 8904-10803/com.lifepage.jfh V/FA: Using measurement service 08-14 10:22:48.611 8904-10803/com.lifepage.jfh V/FA: Connecting to remote service 08-14 10:22:48.615 8904-10803/com.lifepage.jfh V/FA: Activity paused, time: 1208442468 08-14 10:22:48.618 8904-10803/com.lifepage.jfh D/FA: Logging event (FE): user_engagement(_e), Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=119948, firebase_screen_class(_sc)=TutorialActivity, firebase_screen_id(_si)=-4275405240345914240}] 08-14 10:22:48.641 8904-10803/com.lifepage.jfh V/FA: Using measurement service 08-14 10:22:48.641 8904-10803/com.lifepage.jfh V/FA: Connection attempt already in progress 08-14 10:22:48.641 8904-10803/com.lifepage.jfh D/FA: Connected to remote service 08-14 10:22:48.641 8904-10803/com.lifepage.jfh V/FA: Processing queued up service tasks: 2 08-14 10:22:48.708 19291-10804/? V/FA-SVC: Event recorded: Event{appId='com.lifepage.jfh', name='user_engagement(_e)', params=Bundle[{firebase_event_origin(_o)=auto, engagement_time_msec(_et)=119948, firebase_screen_class(_sc)=TutorialActivity, firebase_screen_id(_si)=-4275405240345914240}]} 08-14 10:22:48.741 3486-3486/? D/SystemServicesProxy: getRecentTasks: com.lifepage.jfh 08-14 10:22:49.664 2779-4232/? I/ActivityManager: Killing 8904:com.lifepage.jfh/u0a212 (adj 900): remove task 08-14 10:22:49.665 2779-4232/? I/ActivityManager: Killing 8956:com.lifepage.jfh:download_queue/u0a212 (adj 904): remove task 08-14 10:22:49.705 2779-3717/? I/WindowManager: WIN DEATH: Window{c7c64b5 u0 com.lifepage.jfh/com.lifepage.jfh.TutorialActivity} 08-14 10:23:04.678 24779-24779/? W/GCM-DMM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=com.lifepage.jfh (has extras) }

freddiN commented 6 years ago

Same issue here: 09-20 22:00:29.320 2485-2485/com.google.android.gms.persistent W/GCM-DMM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=(my package) (has extras) }

I was hoping updating to the lastest libs would fix this, but it does not. AS 2.3.3 om.google.gms:google-services:3.1.1 com.google.firebase:firebase-messaging:11.0.4

privatejava commented 6 years ago

Facing similar issue : 01-31 12:54:57.417: W/GCM-DMM(2638): broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE flg=0x10000000 pkg=np.ngopal.net.cbts.marcopolo (has extras) }

I am using Android Oreo in Oneplus 3 device. I closed the app by swipping away from task list. But if i have app running behind it will show the notification.

rahuldevanavar91 commented 6 years ago

@privatejava This might be helpful for you https://stackoverflow.com/a/45810771/1993001

privatejava commented 6 years ago

Thanks @rahuldevanavar91 It worked after i disable the battery optimization even after i cleared from task list.

But I am amazed how are other apps being able to send notification even if i cleared them via task list when i literally have battery optimization enabled.

rahuldevanavar91 commented 6 years ago

@privatejava ... if battery optimization is enabled other applications also not receive the notification I cheked this, only WhatsApp notification you will get

spddheeraj commented 6 years ago

@rahuldevanavar91 any idea how WhatsApp is doing it?

privatejava commented 6 years ago

@spddheeraj May be they will try to create a background services app once they gets killed manually . So they would always get notification.

daveabes commented 6 years ago

Also getting this issue, is there any resolutions at this time?

fajaranugrah commented 6 years ago

@privatejava can you help me how to create service running on FirebaseMessagingService?

najeira commented 5 years ago

Why does not SDK call onMessageReceived in background? I can not find a reasonable reason.

taranjeetsapra commented 5 years ago

I found a related topic stating that we can send only notification messages through Firebase Console. If we want a datapayload message we can send that using Postman.

{
"to" : "token of client",
"collapse_key" : "type_a",
"data" : {
     "body" : "Test",
    "title": "Test",
    "message" : "Test"
}
 }

Please note that we have not sent "notification" here as we only want the payload to be given. Reference: https://stackoverflow.com/questions/37454501/firebase-fcm-force-ontokenrefresh-to-be-called