Reputation: 67
I am using Firebase functions to do the push notifications, and it works well. But after receiving notifications when I click on a certain type of notifications, it takes me to phone setting (Wifi setting, or application chooser dialog depending on phone I am testing on) instead of opening up the application. Below are my code for FirebaseMessagingService.
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(TAG, "onMessageReceived: url is " + remoteMessage.getData().get("avatar_url"));
myRef = FirebaseDatabase.getInstance().getReference();
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getApplicationContext(), channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(remoteMessage.getData().get("title"))
.setContentText(remoteMessage.getData().get("body"))
.setLargeIcon(ImageManager.getBitmapFromUrl(remoteMessage.getData().get("avatar_url")));
type = remoteMessage.getData().get("type");
userName = remoteMessage.getData().get("title");
Log.d(TAG, "onMessageReceived: username is " + userName);
uid = remoteMessage.getData().get("uid");
Log.d(TAG, "onMessageReceived: uid is " + uid);
chatId = remoteMessage.getData().get("chatId");
Log.d(TAG, "onMessageReceived: chatId is " + chatId);
notificationId = remoteMessage.getData().get("notificationId");
Log.d(TAG, "onMessageReceived: notification id is " + notificationId);
if(type.equals("message")){
Intent resultIntent = new Intent("android.intent.action.MESSAGING");
resultIntent.putExtra("chatId", chatId);
resultIntent.putExtra("uid", uid);
resultIntent.putExtra("userName", userName);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
getApplicationContext(),
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setAutoCancel(true);
myRef.child("notifications")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.child(notificationId)
.setValue(null);
} else {
//Getting problem here
Intent resultIntent = new Intent("android.intent.action.MAIN");
resultIntent.putExtra("FragmentIndicator", "2");
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
getApplicationContext(),
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setAutoCancel(true);
myRef.child("notifications")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.child(notificationId)
.setValue(null);
}
int mNotificationId = (int) System.currentTimeMillis();
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
For this logic if(type.equals("message"))
, it all works fine. But the problem is with the else
statement which includes notification types of friend request or friend request confirmation in which cases I am intent to direct users to my Chat Fragment.
My manifest for the main activity is like this:
<activity
android:name=".HomeActivity"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Any ideas what went wrong here? Much appreciated.
Upvotes: 1
Views: 139
Reputation: 11018
you are giving the system level intents to the pending intent so it's opening the system settings. Instead, you should give your HomeActivity.class or any other activity from your application defined in the manifest of your application.
here you go.
if(type.equals("message")){
Intent resultIntent = new Intent(getApplicationContext(),HomeActivity.class);
resultIntent.putExtra("chatId", chatId);
resultIntent.putExtra("uid", uid);
resultIntent.putExtra("userName", userName);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
getApplicationContext(),
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setAutoCancel(true);
myRef.child("notifications")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.child(notificationId)
.setValue(null);
} else {
//Getting problem here
Intent resultIntent = new Intent(getApplicationContext(),HomeActivity.class);
resultIntent.putExtra("FragmentIndicator", "2");
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
getApplicationContext(),
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
mBuilder.setAutoCancel(true);
myRef.child("notifications")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.child(notificationId)
.setValue(null);
}
Upvotes: 1