android - Push notification works incorrectly when app is on background or not running -


i using firebase cloud messaging send push notifications.

here firebasemessageservice:

public class firebasemessageservice extends firebasemessagingservice {   @override public void onmessagereceived(remotemessage remotemessage) {     log.e("tag", "from: " + remotemessage.getfrom());     log.e("tag", "notification message body: " + remotemessage.getdata().get("cardname")+"  :  "+remotemessage.getdata().get("cardcode"));     sendnotification(remotemessage.getnotification().getbody()); }   private void sendnotification(string messagebody) {     intent intent = new intent(this, startactivity.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.mipmap.ic_launcher_final)             .setcontenttitle("notification")             .setcontenttext(messagebody)             .setticker("test")             .setautocancel(true)             .setdefaults(notification.default_sound)             .setcontentintent(pendingintent);      notificationmanager notificationmanager =             (notificationmanager) getsystemservice(context.notification_service);          notificationmanager.notify(0 /* id of notification */, notificationbuilder.build());     } } 

and firebaseinstanceserver:

public class firebaseinstanceservice extends firebaseinstanceidservice {   @override public void ontokenrefresh() {     // updated instanceid token.     string refreshedtoken = firebaseinstanceid.getinstance().gettoken();     log.e("tag", "refreshed token: " + refreshedtoken);      // todo: implement method send registration app's servers.     sendregistrationtoserver(refreshedtoken);  }  private void sendregistrationtoserver(string token) {         // add custom implementation, needed.         log.e("tag", "refreshed token2: " + token);     } } 

which declared in androidmanifest:

<service     android:name=".util.notifications.firebasemessageservice">     <intent-filter>         <action android:name="com.google.firebase.messaging_event"/>     </intent-filter> </service>  <service     android:name=".util.notifications.firebaseinstanceservice">     <intent-filter>         <action android:name="com.google.firebase.instance_id_event"/>     </intent-filter> </service> 

so issue when app running ticker shown , notification come default sound, when app in foreground or not running notification come without sound , ticker not shown in status bar.
why happening , how can fix it?

with fcm specify post payload send https://fcm.googleapis.com/fcm/send. in payload can specify data or notification key, or both.

if payload contains data key, app handle push messages itself. e.g. delivered onmessagereceived handler.

if payload contains notification key, app handle push messages only if app active/in foreground. if not (so it's in background, or closed entirely), fcm handles showing notification using values put notification key payload.

note notifications sent console (like firebase console), include notification key.

looks want handling fcm messages can customize notification bit more etc, better not include notification key in post payload, push messages delivered onmessagereceived.

you can read more here:
advanced messaging options
downstream message syntax


Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -