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'
}