2

I am just adding chat Functionality into my app after adding core function of apps.I start implementing Firebase push notification into app and follow all steps from official documentation.So whenever i send message from Firebase notification console it shows when app in foreground but when app in background it shows these lines in logcat but no notification

09-27 16:11:37.645 19946-19946/com.example.com.appD/dalvikvm: DexOpt: couldn't find field Landroid/os/Message;.sendingUid
09-27 16:11:37.645 19946-19946/com.example.com.appW/dalvikvm: VFY: unable to resolve instance field 135
09-27 16:11:37.645 19946-19946/com.example.com.appD/dalvikvm: VFY: replacing opcode 0x52 at 0x0000

Here is my Firebase Instance ID Service class

public class FireBase_InstanceID_Service extends FirebaseInstanceIdService {
    public static final String TAG="==Firebase ID===";
    private static final String SubscribeTopic="Chat";
    String refreshedToken;

    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();
        refreshedToken= FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG,"Here Is Token "+refreshedToken);
        FirebaseMessaging.getInstance().subscribeToTopic(SubscribeTopic);

    }
    private void sendRegistrationToServer(String Token){
        //TODO: Send Token To APP server
    }
}

Here is my Firebase Messaging Service class

 public class FireBase_Messaging_Service extends FirebaseMessagingService {
        public static final String TAG="==FireBase MSG==";

        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage);
            Log.d(TAG,"From "+remoteMessage.getFrom());

            if (remoteMessage.getData().size()>0){
                Log.d(TAG,"Message Data "+remoteMessage.getData());
            }
            if (remoteMessage.getNotification()!=null){
                Log.d(TAG,"Message Notification Body "+remoteMessage.getNotification().getBody());
            }
            Log.d(TAG,"FCM Message ID "+remoteMessage.getMessageId());
            Log.d(TAG,"FCM Notification Message: "+remoteMessage.getNotification());
            Log.d(TAG,"FCM Data Message "+remoteMessage.getData());
            showNotification(remoteMessage.getNotification().getBody());
        }

        private void showNotification(String Message){
            Intent intent=new Intent(this, UserProfile.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent=PendingIntent.getActivity(this,5,intent,PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.Builder builder= (NotificationCompat.Builder) new NotificationCompat.Builder (this)
                    .setAutoCancel(true)
                    .setContentTitle("New Notification")
                    .setContentText(Message)
                    .setSmallIcon(R.drawable.common_google_signin_btn_icon_light_normal)
                    .setContentIntent(pendingIntent);

            NotificationManager manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            manager.notify(5,builder.build());

        }
    }

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.com.app">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">
        <activity
            android:name=".Navigation_Drawer"
            android:theme="@style/AppTheme">

            <intent-filter>
                <action android:name="android.intent.action.MAIN"></action>
                <category android:name="android.intent.category.LAUNCHER"></category>
            </intent-filter>


        </activity>

        <service
            android:name=".ImageDownloading"
            android:enabled="true"
            android:exported="false">

        </service>

<!--FIREBASE SERVICES -->

        <service android:name=".FireBase_Messaging_Service">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT"></action>
            </intent-filter>
        </service>

        <service android:name=".FireBase_InstanceID_Service">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"></action>
            </intent-filter>
        </service>

 <!--FIREBASE SERVICES -->

    </application>

</manifest>

So for me i am unable to get push notification in Notification Tray in background or when app is closed With these entries in logcat (which i don't understand)

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
androidXP
  • 1,692
  • 3
  • 27
  • 58

1 Answers1

0

This is working as intended, notification messages are delivered to your onMessageReceived callback only when your app is in the foreground. If your app is in the background or closed then a notification message is shown in the notification center, and any data from that message is passed to the intent that is launched as a result of the user tapping on the notification.

You can specify a click_action to indicate the intent that should be launched when the notification is tapped by the user. The main activity is used if no click_action is specified.

When the intent is launched you can use the

getIntent().getExtras();

to retrieve a Set that would include any data sent along with the notification message.

For more on notification message see docs.

Mohit Trivedi
  • 699
  • 3
  • 13
  • 1
    First i need to get notification in Notification tray then handling data.Please read question again i am not getting notification when app is close or in background – androidXP Sep 27 '16 at 11:32