gpt4 book ai didi

android - 收到通知问题并打开 fragment

转载 作者:行者123 更新时间:2023-11-29 23:31:36 25 4
gpt4 key购买 nike

当用户收到好友请求通知并单击通知时,我正在处理应用程序通知,应用程序必须将他重定向到发送请求的用户个人资料。

我面临的不仅仅是一个问题:首先,如果应用程序在后台运行,接收方可以收到通知,但如果应用程序正在运行,则接收方应用程序在发送方发送通知后崩溃并且不显示通知。

第二个问题是,当接收者点击通知时,应用程序打开并停留在默认 fragment 的主 Activity 中。在我的应用程序中知道主要 Activity 包含多个。我为用户个人资料创建了一个 fragment ,我必须传递一个用户 ID 才能打开这个 fragment ,所以它会定向到发送好友请求的用户个人资料,但似乎没有收到用户 ID。

她是类 FirebaseMessagingService 的代码:

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);

String notification_title = remoteMessage.getNotification().getTitle();
String notification_message = remoteMessage.getNotification().getBody();

String click_action = remoteMessage.getNotification().getClickAction();
String from_user_id = remoteMessage.getData().get("from_user_id");

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
.setContentTitle(notification_title)
.setContentText(notification_message);

Intent intent = new Intent(click_action);
intent.putExtra("user_id", from_user_id);
intent.putExtra("From", "notifyFrag");

PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);

int mNotificationId = (int) System.currentTimeMillis();
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}

在主要 Activity 中:

    @Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
}

@Override
protected void onResume() {
super.onResume();

if(getIntent().getExtras() != null) {

String from_user_id = getIntent().getStringExtra("user_id");
String type = getIntent().getStringExtra("From");

android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

if (type != null) {
switch (type) {
case "notifyFrag":
UserProfileFragment fragment = new UserProfileFragment();
transaction.replace(R.id.fragment_container, fragment);

Bundle bundle = new Bundle();
bundle.putString("user_id", from_user_id);
fragment.setArguments(bundle);

transaction.commit();
}
}
}
}

崩溃错误是:

enter image description here它指向:mNotifyMgr.notify(mNotificationId, mBuilder.build());在类 FirebaseMessagingService 中。

所以,如果有人可以提供帮助,请多多指教!


我更新了类 FirebaseMessagingService 代码,现在应用程序不会崩溃,无论应用程序正在运行还是关闭,我都能在这两种情况下收到通知。但是如果我点击通知什么也不会发生

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);

NotificationCompat.Builder mBuilder;
Intent intent;
PendingIntent pendingIntent;

final int NOTIFY_ID = (int) System.currentTimeMillis(); // ID of notification
String notification_id = getString(R.string.default_notification_channel_id); // default_channel_id

String notification_title = remoteMessage.getNotification().getTitle();
String notification_message = remoteMessage.getNotification().getBody();

String click_action = remoteMessage.getNotification().getClickAction();
String from_user_id = remoteMessage.getData().get("from_user_id");

if(mNotifyMgr == null){
mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = mNotifyMgr.getNotificationChannel(notification_id);

if (mChannel == null) {
mChannel = new NotificationChannel(notification_id, notification_title, importance);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mNotifyMgr.createNotificationChannel(mChannel);
}
mBuilder = new NotificationCompat.Builder(this, notification_id)
.setSmallIcon(R.drawable.logo_ic)
.setContentTitle(notification_title)
.setContentText(notification_message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
intent = new Intent(click_action);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("user_id", from_user_id);
intent.putExtra("From", "notifyFrag");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

pendingIntent =
PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
}
else {
mBuilder = new NotificationCompat.Builder(this, notification_id)
.setSmallIcon(R.drawable.logo_ic)
.setContentTitle(notification_title)
.setContentText(notification_message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
intent = new Intent(click_action);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("user_id", from_user_id);
intent.putExtra("From", "notifyFrag");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

pendingIntent =
PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendingIntent);
}

//Set an ID for the notification
mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// notificationId is a unique int for each notification that you must define
mNotifyMgr.notify(NOTIFY_ID, mBuilder.build());
}

最佳答案

您收到错误消息是因为您没有在通知中添加小图标

  .setSmallIcon(R.drawable.your_icon)

更新代码

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))
.setContentTitle(notification_title).setSmallIcon(R.drawable.your_icon)
.setContentText(notification_message);

developer , oneSignal

关于android - 收到通知问题并打开 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52576771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com