1

I'm developing an application in which I send notifications through Firebase. I've followed several tutorials in which the idea is to open an activity when I press the notification. This action works correctly as long as the App is open, but if it is closed or "minimized" and press the notification it is opened from the default activity, which in my case would be the SplashScreen, I really don't explain why it doesn't work. Is there anything you can recommend?

The following would be the class we know as MyFirebaseMessagingService. As you can see when I press the notification, it takes me to NotificationActivity.class and it does... but only if the application is open.

package com.asecum.com.campussicapp;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by asecum5 on 24/08/17.
 */

public class Firebase_NotificationService extends FirebaseMessagingService {

    private static final String TAG = "NOTICIAS";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        /*String from = remoteMessage.getFrom();
        Log.d(TAG, "Mensaje recibido de: " + from);

        if(remoteMessage.getNotification() != null) {
            Log.d(TAG, "Notification: " + remoteMessage.getNotification().getBody());

            showNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody());
        }
        if(remoteMessage.getData().size()>0){
            Log.d(TAG, "Data: " + remoteMessage.getData());
        }*/
        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Data: " + remoteMessage.getData());
            try {
                JSONObject data = new JSONObject(remoteMessage.getData());
                String jsonMessage = data.getString("extra_information");
                Log.d(TAG, "onMessageReceived: \n" + "Extra information: " + jsonMessage);

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        if (remoteMessage.getNotification() != null) {
            String title = remoteMessage.getNotification().getTitle();
            String message = remoteMessage.getNotification().getBody();
            showNotification(title, message);
        }
    }
    private void showNotification(String title, String body) {
        Intent intent = new Intent(this, NotificationActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

        Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_home_black_24dp)
                .setContentTitle(title)
                .setContentText(body)
                .setAutoCancel(true)
                .setVibrate(new long[]{100, 250, 100, 250, 100, 250})
                .setSound(soundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, notificationBuilder.build());
    }
}

The next is the manifest... Could be possible that i forgot some permission?

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

    <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:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">


        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="MAINACTIVITY" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name=".SplashScreen"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>


        <activity
            android:name=".NotificationActivity">
            <intent-filter>
                <action android:name="NOTIFICATIONACTIVITY" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>


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

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

        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />
    </application>

</manifest>

This is what I added in gradle

Module's build.gradle

dependencies {
   ...

    compile 'com.google.firebase:firebase-core:10.2.6'
    compile 'com.google.firebase:firebase-messaging:10.2.6'
    testCompile 'junit:junit:4.12'
}

apply plugin: `com.google.gms.google-services`

Project's build.gradle

dependencies {
   ...
classpath 'com.google.gms:google-services:3.1.0'
}
Vishal Yadav
  • 3,351
  • 3
  • 20
  • 40

1 Answers1

1

I think this is caused by the difference between "data" style cloud messages and "notification" style cloud messages in Firebase.

For a notification style message:

FCM automatically displays the message to end-user devices on behalf of the client app.

Also note that the firebase console can send only the notification style of message.

Could it be that this notification style cloud message arrives and is being handled automatically by the android system and not by your app (when the app is closed or in the background)?

If that is the case, then the system will open the app's default activity (splash screen) when the user clicks the notification.

As an alternative, you can send a "data" style cloud message which will be handled by your app instead of by the system. Then when the user clicks the notification, it will execute the code defined in your .Firebase_NotificationService

From a Mac command line, you can use an a script like this to send a data style message through Google's servers to a particular app instance id:

curl -X POST \
--Header "Authorization: key=<get authorization key from Firebase console - Project Settings - Web API Key>” \
--Header "Content-Type: application/json" https://fcm.googleapis.com/fcm/send  \
-d " \
    { \
        \"to\”:\”<get id for your particular android device app instance from Firebase Instance ID service in your app>\”, \
        \"data\": \
        { \
            \"title\":\" test title here \", \
            \"body\":\" test body here \"
        }, \
        \"priority\": \"normal\" \
    }"

Notice that there is no "notification" block in the JSON.

See also this and this

albert c braun
  • 2,470
  • 20
  • 32
  • Thanks for help me... I was reading about that problem between data and notification and the Firebase documentation too. I see that happen because the android system has that action by default. Do you know how can I send just the data style cloud message? Do i have to do that in the Firebase Console or in Android Studio? Thank you in advance – Juan D. Godoy Aug 29 '17 at 17:54
  • One way to send a data style message is from the command line, using curl: https://stackoverflow.com/a/37376757/3482621 – albert c braun Aug 29 '17 at 18:07
  • Another way is using a Java program: https://stackoverflow.com/questions/37426542/firebase-cloud-messaging-notification-from-java-instead-of-firebase-console – albert c braun Aug 29 '17 at 18:09