Notification come up in notification center with image (even app is closed | app is in background) and whenever I click notification, the activity open it but no intent data.
I always get null whenever I try to get intent extra. No matter I try hard to pass data through Bundle or directly passing in intent.setExtra, I always get null.
I have a Firebase handler class
public class MyFirebaseInstanceService extends FirebaseMessagingService {
@Override
public void onNewToken(String s) {
super.onNewToken(s);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//super.onMessageReceived(remoteMessage);
String msg = remoteMessage.getNotification().getBody();
String title = remoteMessage.getNotification().getTitle();
String channel = remoteMessage.getNotification().getChannelId();
Holder holder = new Holder();
holder.setTitle(title);
holder.setMsg(msg);
holder.setChannel(channel);
if (remoteMessage.getData().size() > 0) {
Map<String, String> params = remoteMessage.getData();
holder.setId(params.get("id"));
holder.setImgUrl(params.get("imgurl"));
holder.setType(params.get("type"));
} else {
int r = new Random().nextInt();
holder.setId(r + "");
holder.setType("bigtext");
}
sendMyNotification(holder, remoteMessage);
}
private void sendMyNotification(Holder holder, RemoteMessage remoteMessage) {
String clickAction = remoteMessage.getNotification().getClickAction();
Intent intent = new Intent(this, NotificationReadActivity.class);
if (clickAction != null) {
if (!clickAction.isEmpty() && clickAction.length() > 5)
intent = new Intent(clickAction);
}
intent.putExtra("obj", holder);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setAction(holder.getId() + "");
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, holder.getChannel());
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
notificationBuilder.setChannelId(holder.getChannel());
if (holder.getType().equals("bigpic")) {
notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle());
Bitmap bitmap = getBitmapfromUrl(holder.getImgUrl());
if (bitmap != null) {
notificationBuilder.setLargeIcon(bitmap);
}
} else {
notificationBuilder.setStyle(new NotificationCompat.BigTextStyle());
}
notificationBuilder.setContentTitle(holder.getTitle());
notificationBuilder.setContentText(holder.getMsg());
PendingIntent pendingIntent = PendingIntent.getActivity(this, holder.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(holder.getChannel(), "Default", NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true);
channel.enableVibration(true);
channel.setLightColor(Color.RED);
channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(holder.getId(), notificationBuilder.build());
} catch (Exception ignored) {
}
}
private Bitmap getBitmapfromUrl(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (Exception ignored) {
return null;
}
}
}
then I have a one activity
public class NotificationReadActivity extends AppCompatActivity {
TextView title, desc;
AppCompatImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_read);
title = findViewById(R.id.title);
desc = findViewById(R.id.desc);
img = findViewById(R.id.img);
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
handleIntent(intent);
}
private void handleIntent(Intent intent) {
Bundle bundle = intent.getExtras();
if (intent.hasExtra("obj")) {
Holder holder = (Holder) intent.getSerializableExtra("obj");
if (holder.getType().equals("bigpic")) {
Glide.with(this).load(holder.getImgUrl()).into(img);
} else
img.setVisibility(View.GONE);
title.setText(holder.getTitle());
desc.setText(holder.getMsg());
} else if (bundle.containsKey("obj")) {
Holder holder = (Holder) bundle.getSerializable("obj");
if (holder.getType().equals("bigpic")) {
Glide.with(this).load(holder.getImgUrl()).into(img);
} else
img.setVisibility(View.GONE);
title.setText(holder.getTitle());
desc.setText(holder.getMsg());
} else {
title.setText("Null");
//finish();
}
}
}
in manifest I have also set properly
<activity
android:name=".NotificationReadActivity"
android:excludeFromRecents="true"
android:label="@string/notification_center"
android:launchMode="singleTop">
<intent-filter>
<action android:name="com.full_package.NotificationReadActivity.TARGET_NOTIFICATION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<service
android:name=".Fcm.MyFirebaseInstanceService"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</service>
I have come up with the only solution is a passing string or int or any type of data other than an object in the intent will work. intent.putExtra can not handle or pass an object.
Intent intent = new Intent(this, NotificationReadActivity.class);
intent.putExtra("key1", "some string"); //this is correct
intent.putExtra("key2", 123); //this is correct
intent.putExtra("key3", any type of data); //this is correct
intent.putExtra("object", some_object); //this is in-correct