I'm not able to open specific activities via specific Notification in my signed APK. It works perfectly when we run app from PC in Android Studio.
This is what I can achieve on Android Studio:
Send the push notification via Firebase Console, then if it contains a specific key-value pair (e.g. timetable
, engineering
), then Timetable activity opens.
This works fine on Android Studio, but not in debuggable and release signed APK.
In deuggable and signed APK, the main activity opens every time when we send specified key-value pairs like timetable
as key and engineering
as value.
Following code is not executing in signed and debuggable apk. MainActivity Code:
if (getIntent().getExtras() != null) {
for (String key : getIntent().getExtras().keySet()) {
String value = getIntent().getExtras().getString(key);
if (key.equals("timetable") && value.equals("engineering")) {
Intent intent = new Intent(this, Timetable.class);
intent.putExtra("value", value);
startActivity(intent);
finish();
}
else if(key.equals("timetable") && value.equals("science")) {
Intent intent = new Intent(this, Timetable.class);
intent.putExtra("value", value);
startActivity(intent);
finish();
}
else if (key.equals("timetable") && value.equals("law")) {
Intent intent = new Intent(this, Timetable.class);
intent.putExtra("value", value);
startActivity(intent);
finish();
}
else if (key.equals("timetable") && value.equals("management")) {
Intent intent = new Intent(this, Timetable.class);
intent.putExtra("value", value);
startActivity(intent);
finish();
}
else if (key.equals("timetable") && value.equals("pharmacy")) {
Intent intent = new Intent(this, Timetable.class);
intent.putExtra("value", value);
startActivity(intent);
finish();
}
else if (key.equals("timetable") && value.equals("mmss")) {
Intent intent = new Intent(this, Timetable.class);
intent.putExtra("value", value);
startActivity(intent);
finish();
}
else if (key.equals("timetable") && value.equals("commerce")) {
Intent intent = new Intent(this, Timetable.class);
intent.putExtra("value", value);
startActivity(intent);
finish();
}
else if (key.equals("timetable") && value.equals("education")) {
Intent intent = new Intent(this, Timetable.class);
intent.putExtra("value", value);
startActivity(intent);
finish();
}
else if (key.equals("result") && value.equals("rengineering")) {
Intent intent = new Intent(this, Results.class);
intent.putExtra("value", value);
startActivity(intent);
finish();
}
else if (key.equals("result") && value.equals("rscience")) {
Intent intent = new Intent(this, Results.class);
intent.putExtra("value", value);
startActivity(intent);
finish();
}
else if (key.equals("result") && value.equals("rarts")) {
Intent intent = new Intent(this, Results.class);
intent.putExtra("value", value);
startActivity(intent);
finish();
}
else if (key.equals("result") && value.equals("rlaw")) {
Intent intent = new Intent(this, Results.class);
intent.putExtra("value", value);
startActivity(intent);
finish();
}
else if (key.equals("result") && value.equals("rmanagement")) {
Intent intent = new Intent(this, Results.class);
intent.putExtra("value", value);
startActivity(intent);
finish();
}
else if (key.equals("result") && value.equals("rpharmacy")) {
Intent intent = new Intent(this, Results.class);
intent.putExtra("value", value);
startActivity(intent);
finish();
}
else if (key.equals("result") && value.equals("rmmss")) {
Intent intent = new Intent(this, Results.class);
intent.putExtra("value", value);
startActivity(intent);
finish();
}
else if (key.equals("result") && value.equals("rcommerce")) {
Intent intent = new Intent(this, Results.class);
intent.putExtra("value", value);
startActivity(intent);
finish();
}
else if (key.equals("result") && value.equals("reducation")) {
Intent intent = new Intent(this, Results.class);
intent.putExtra("value", value);
startActivity(intent);
finish();
}
}
}
FirebaseMessagingService Class
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
}
//The message which i send will have keys named [message, image, AnotherActivity] and corresponding values.
//You can change as per the requirement.
//message will contain the Push Message
String message = remoteMessage.getData().get("message");
//String imageUri = remoteMessage.getData().get("image");
//If the key AnotherActivity has value as True then when the user taps on notification, in the app AnotherActivity will be opened.
//If the key AnotherActivity has value as False then when the user taps on notification, in the app MainActivity will be opened.
String TrueOrFlase = remoteMessage.getData().get("AnotherActivity");
//To get a Bitmap image from the URL received
sendNotification(message, bitmap, TrueOrFlase);
}
/**
* Create and show a simple notification containing the received FCM message.
*/
private void sendNotification(String messageBody, Bitmap image, String TrueOrFalse) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("AnotherActivity", TrueOrFalse);
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)
.setLargeIcon(image)/*Notification icon image*/
.setSmallIcon(R.drawable.icon)
.setContentTitle(messageBody)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(image))/*Notification with Image*/
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
/*
*To get a Bitmap image from the URL received
* */
}