java - 通过 Firebase 点击通知获得 Intent Extra
问题描述
通知出现在带有图像的通知中心(即使应用程序已关闭|应用程序在后台),每当我单击通知时,活动都会打开它但没有意图数据。
每当我试图获得额外的意图时,我总是会得到空值。无论我努力通过Bundle传递数据还是直接传入intent.setExtra,我总是得到null。
我有一个 Firebase 处理程序类
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;
}
}
}
然后我有一个活动
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();
}
}
}
在清单中我也正确设置了
<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>
解决方案
我想出的唯一解决方案是传递字符串或 int 或任何类型的数据,而不是意图中的对象。intent.putExtra 无法处理或传递对象。
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
推荐阅读
- python - 缩放 Pygame 显示表面上的所有内容
- python - 在 sqlite3 中插入变量后如何使用 WHERE
- java - ANDROID 无法连接蓝牙:读取失败,套接字可能关闭或超时,读取 ret:-1
- javascript - 如何获取与下拉列表中选择的列关联的整行?
- python - 如果列没有名称且仅包含“rangeindex”,如何使用 pandas set_index
- r - 关于重命名函数的问题
- python - Python 在函数中使用类并附加到列表
- python - 离线“conda install”库和依赖项的方法
- pip - 我在 python/python39/scripts 文件夹中看到 pip 但它仍然显示“找不到 pip 命令”
- android - 如何共享位于下载目录中的文件?