0

I have tried some solution from the stackoverflow . But not working properly in my case. Below is my MyFirebaseMessagingService class which extends FirebaseMessagingService

public class MyFirebaseMessagingService   extends FirebaseMessagingService {

    String url , title , body ;

    Map<String,String> data = new HashMap<>();

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {


        title = remoteMessage.getNotification().getTitle();
        body = remoteMessage.getNotification().getBody();

        if(remoteMessage.getData().size()>0){
            Log.d("DATA", remoteMessage.getData().toString());
            try{
                data = remoteMessage.getData();
                url = data.get("url");
            }
            catch (Exception e){
                Log.e("Error", e.toString());
            }

            showNotification(url , title , body);
        }






    }

    private void showNotification(String url, String title, String body) {
        Intent intent = new Intent(this, NotificationViewer.class);
        intent.putExtra("URL",url);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) ;
        notificationBuilder.setContentTitle(title);
        notificationBuilder.setContentText(body);
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSmallIcon(R.drawable.logo);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notificationBuilder.build());

    }
}

Below is my AndroidManifest.xml code .

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

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


    <application
        android:name=".Controller.AppController"
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".activity.SplashActivity"
            android:noHistory="true"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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


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

        <activity android:name=".activity.NotificationViewer"
            android:exported="true">
        </activity>
    </application>

</manifest>

I have tried out some solution but not working in my case. Everything is working fine when app is foreground but not working in background. I want to open a particular activity name NotificationViewer.Java activity when user click on the notification. But when my app is in background it keep opening my Launcher Activity.

KENdi
  • 7,576
  • 2
  • 16
  • 31
Piash Sarker
  • 466
  • 5
  • 21
  • It's because when app is in background notifications are administrated by the system by default , FirebaseMessagingService is never called : look my answer here: (http://stackoverflow.com/a/42343510/3812081) – venimania Apr 11 '17 at 12:18

2 Answers2

1

You need to set click action in your json payload in notification request 'click_action' => 'YourTAG' and apply same tag in your manifest activity

<activity
      android:name=".activity.NotificationViewer">
      <intent-filter>
             <action android:name="YourTAG" />  
             <category android:name="android.intent.category.DEFAULT"/>              
      </intent-filter>
</activity>
Jd Prajapati
  • 1,953
  • 13
  • 24
  • thanks for your comment. Should bind this like below : `"data" : { "url" : "https://www.google.com", "name": "Piash", "click_action" : "tag" }` – Piash Sarker Apr 11 '17 at 12:15
  • you need to add tag inside notification object, e.g.: "click_action":"YourTAG" from server side – Jd Prajapati Apr 11 '17 at 12:19
  • I edit my AndroidManifest.xml like below: ` ` And add ` "data" : { "click_action": "action_tag" }` But still not working, – Piash Sarker Apr 11 '17 at 12:22
  • its not working becuase you are adding inside data obejct, you need to add inside notification object...<<"notification": {"title": "Click Action Message","text": "Sample message","click_action":"action_tag"}>> – Jd Prajapati Apr 11 '17 at 12:25
  • thanks for your solution. I added it in notification object . Now it's working fine. Thanks – Piash Sarker Apr 11 '17 at 12:25
  • i have faced another problem , i am sending a url in my data object but this url is not get when i open notificatoin. my json format is below: `"to": "/topics/news", "notification" : { "body" : "great match! ", "title" : "Portugal vs. Denmark", "click_action": "action_tag" }, "data" : { "url" : "https://www.google.com", "name": "Piash" } }` – Piash Sarker Apr 11 '17 at 12:35
  • you need to getvalue from your bundle. – Jd Prajapati Apr 11 '17 at 12:37
  • So should i use Bundle instead of putExtra() to pass my data from MyFirebaseMessagingService to NotificationView class. – Piash Sarker Apr 11 '17 at 12:40
  • no, on background it will automatically send in bungle, you need check bundle is null or not, if not then fetch your value from bundle. – Jd Prajapati Apr 11 '17 at 12:44
  • thanks @Jd . I got the point . Every value is sent to notification as an key value of json . – Piash Sarker Apr 11 '17 at 13:14
0

You are most likely using the Firebase Notification message instead of the Data message.

If this is the case, using Data FCM message should do the trick for you.

As can be seen in https://firebase.google.com/docs/cloud-messaging/concept-options

Use notification messages when you want FCM to handle displaying a notification on your client app's behalf. Use data messages when you want to process the messages on your client app.

Notification Messages:

When your application is in the foreground, it receives the payload, but when in the background, Notification messages are delivered to the notification tray.

Data Messages: These are are always delivered to the application, regardless if it is in the foreground or in the background.

NOTE: The FCM data message alone will not bring your application in the foreground. You will need additional code that will do that upon reception of the data message.