android - onMessageReceived() is not called when app is in foreground -


i using following code receive fcm message sent through firebase console, fucntion never called. instead able receive message in launcher class when app in background, when app in foreground , not able it.

public class myfirebasemessagingservice extends firebasemessagingservice {  private static final string tag = "myfirebasemsgservice"; string message=""; map<string, string> m1;  @override public void onmessagereceived(remotemessage remotemessage) {     //displaying data in log     //it optional     log.d(tag, "from: " + remotemessage.getfrom());     log.d(tag, "notification message body: " + remotemessage.getnotification().getbody());     system.out.println("---------------------");      message = remotemessage.getnotification().gettitle();     m1= remotemessage.getdata();     system.out.println("=============="+m1);     system.out.println("=============="+m1.get("url"));      //calling method generate notification     //sendnotification(remotemessage.getnotification().getbody());     //sendnotification_test(message);    createnotification(m1,remotemessage.getnotification().getbody()); }   private void createnotification(map<string,string> payload, string title){      intent intent = new intent(this, loginactivity.class);       intent.putextra("url",payload.get("url"));     intent.putextra("package",payload.get("package"));     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)             .setcontenttitle("fcm message")             .setcontenttext(title)             .setautocancel(true)             .setsound(defaultsounduri)             .setcontentintent(pendingintent);      notificationmanager notificationmanager =             (notificationmanager) getsystemservice(context.notification_service);      notificationmanager.notify(0 /* id of notification */, notificationbuilder.build()); } } 

manifest

 <service     android:name="com.example.pranshusrivastav.fcmtest.myfirebasemessagingservice">     <intent-filter>         <action android:name="com.google.firebase.messaging_event"/>     </intent-filter> </service>  <service     android:name="com.example.pranshusrivastav.fcmtest.myfirebaseinstanceidservice">     <intent-filter>         <action android:name="com.google.firebase.instance_id_event"/>     </intent-filter> </service> 

i putting service declaration out of application tag in manifest:

<application         android:allowbackup="true"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:supportsrtl="true"         android:theme="@style/apptheme">         <activity             android:name=".loginactivity"             android:label="@string/app_name">             <intent-filter>                 <action android:name="android.intent.action.main" />                  <category android:name="android.intent.category.launcher" />             </intent-filter>         </activity>          <service             android:name="com.example.pranshusrivastav.fcmtest.myfirebasemessagingservice">             <intent-filter>                 <action android:name="com.google.firebase.messaging_event"/>             </intent-filter>         </service>          <service             android:name="com.example.pranshusrivastav.fcmtest.myfirebaseinstanceidservice">             <intent-filter>                 <action android:name="com.google.firebase.instance_id_event"/>             </intent-filter>         </service>     </application> 

this worked !!


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

angular - Is it possible to get native element for formControl? -

javascript - Why jQuery Select box change event is now working? -