Skip to content
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

onMessageRecieved not working when app is killed. #365

Closed
DanikKamilov opened this issue Nov 3, 2017 · 12 comments
Closed

onMessageRecieved not working when app is killed. #365

DanikKamilov opened this issue Nov 3, 2017 · 12 comments
Assignees

Comments

@DanikKamilov
Copy link

DanikKamilov commented Nov 3, 2017

onMessageRecieved not working when app is killed. It works perfect in foreground and in background and I can receive Extras in MainActivity. But when app is killed I receive notification and after click on it- Extras == null.

I send only DATA (to, data) notification. What I do wrong?

@kroikie kroikie self-assigned this Nov 3, 2017
@kroikie
Copy link
Contributor

kroikie commented Nov 3, 2017

Hi @DanikKamilov could you include a few things that would help?

  • How are you "killing" the app?
  • What device are you using?
  • Are you using the REST API or console to send your message?
    • If you are using the REST API could you include the body of the message you are sending?

@DanikKamilov
Copy link
Author

DanikKamilov commented Nov 3, 2017

Hello.

  1. I closed app by home button and throw off it from tasks(from launched apps). So it full closed now.
  2. Xiaomi redmi note 4, Blackberry , Sony Experia

a) {
"to" : "......My Device Token....",
"data" : {
"body" : "Hello World"
}
}

b) {
"condition" : "'news' in topics",
"data" : {
"body" : "Hello World"
//or "message" or "body" and "message" and so on...
}
}

it works fine for Foreground and Background but not for Closed app

@kroikie
Copy link
Contributor

kroikie commented Nov 3, 2017

Looks like you are using the REST API to send data messages. This means that the notification that you are creating and any data included in it is up to you. FCM is not managing including data in those notifications. Only when you send notification-messages does the FCM SDK play a role in displaying notifications.

I would look at the code you are using to generate the notifications.

@DanikKamilov
Copy link
Author

DanikKamilov commented Nov 3, 2017

`
$path_to_fcm = 'https://fcm.googleapis.com/fcm/send';
$server_key = "ServerKEY";

$headers = array (
'Authorization:key='.$server_key,
'Content-Type:application/json'
);

$data = array(
"message" => $message,
'vibrate'	=> 1,
'sound'		=> 1,
"priority" => "high"
);
      
$final = array(
//"condition" => "'news' in topics",
"to"=>"DeviceToken",
"data" => $data
 );

$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $final ) );

$result = curl_exec($ch );

curl_close( $ch );

`

and yes... problem in Android version. iOS - works fine.

@kroikie
Copy link
Contributor

kroikie commented Nov 3, 2017

@DanikKamilov the issue would be in the code that you are using to generate the notification on the Android device. Your server code seems fine which is why you are not having issues on iOS.

have a look in your onMessageReceived method that is likely where the issue is.

I'm closing this issue now since the issue is not related to Firebase. If you think it is please file a new issue with details.

@kroikie kroikie closed this as completed Nov 3, 2017
@IvanGarza07
Copy link

Hello!!
I have the same issue but the problem not is firebase... i tested my project in samsung, nexus phone and works fine but in huawei doesn't work so probably others provider have the same problems
can you give us any suggestions for this??

@kroikie
Copy link
Contributor

kroikie commented Nov 3, 2017

It would be difficult to make suggestions here without seeing the code used to generate the notification after the data message is received. We are constantly working with device makers to have a consistent experience, if you are getting unexpected behavior for messages on particular devices please let us know make, model and OS version and we will try to address it.

@DanikKamilov
Copy link
Author

DanikKamilov commented Nov 4, 2017

`
@OverRide
public void onMessageReceived(RemoteMessage remoteMessage) {

        Map<String, String> map = remoteMessage.getData();
        String message="";
        for (Map.Entry<String, String> entry : map.entrySet()) {
            message= entry.getValue();
        }
        sendNotification(message);

}

    private void sendNotification(String messageBody) {
     Intent intent = new Intent(this, MainActivity.class);
    intent.putExtra("notification",messageBody);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    String channelId = getString(R.string.default_notification_channel_name);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_notifications_active_black_24dp)
                    .setContentTitle("MyTitle")
                    .setContentText(messageBody)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);

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

    notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

`

and I've tried to use different flags in intent and pending intent and it not works... (I can't catch intent.putExtra("notification",messageBody); in MainActivity when app is closed.)

@DanikKamilov
Copy link
Author

DanikKamilov commented Nov 4, 2017

And one more question. I can't subscribe iOS app to topic. Where in code I need to subscribe ? In AppDelegate?

@bhaskar2342
Copy link

I have an issue with setContentTitle. Im working with flavors in android. I'm comfortably receiving notifications when app is in foreground/background/killed. But the problem is when app is in background/killed the title i'm receiving is incorrect i.e the title i'm getting from server what they've set. If the app is in foreground the title works fine.
For example i have 2 flavors Flavor1 and Flavor2 and set the titles as F1 and F2 respectively and from server side they set the title as TstNotif. When the app is in foreground the title for the notification i'm receiving is F1 or F2 depends on flavor, but when the app is in background i'm receiving TstNotif for both the flavors.
For iOS they are working fine....!

@KORuL
Copy link

KORuL commented Mar 2, 2019

In Meizu M1 Note data-only push comes if app is foreground/background? but if app is killed onMessageRecieved is not call.

@KORuL
Copy link

KORuL commented Mar 2, 2019

error W/GCM: broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE pkg=net.korul.hbbft (has extras) }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants