Java Firebase消息传递错误:致命异常:Firebase消息传递意图句柄
我有问题,但不明白为什么会发生这种情况,我在应用程序中有两种类型的通知,Java Firebase消息传递错误:致命异常:Firebase消息传递意图句柄,java,android,Java,Android,我有问题,但不明白为什么会发生这种情况,我在应用程序中有两种类型的通知,ChatNotification和PostNotification,在添加新帖子后,我收到以下错误: 如果(notificationType.equals(“PostNotification”){ 公共类FirebaseMessaging扩展了FirebaseMessagingService{ private static final String ADMIN_CHANNEL_ID = "admin_channel"; @
ChatNotification
和PostNotification
,在添加新帖子后,我收到以下错误:
如果(notificationType.equals(“PostNotification”){
公共类FirebaseMessaging扩展了FirebaseMessagingService{
private static final String ADMIN_CHANNEL_ID = "admin_channel";
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
//get current user from shared preferences
SharedPreferences sp = getSharedPreferences("SP_USER", MODE_PRIVATE);
String savedCurrentUser = sp.getString("Current_USERID", "None");
/*we have two type notification
> notificationType ="PostNotification"
> notificationType ="ChatNotification"*/
String notificationType = remoteMessage.getData().get("notificationType");
if (notificationType.equals("PostNotification")){
//post notification
String sender = remoteMessage.getData().get("sent");
String pId = remoteMessage.getData().get("id");
String pTitle = remoteMessage.getData().get("name");
String pDescription = remoteMessage.getData().get("description");
//if user is same that has post don't show notification
if (!sender.equals(savedCurrentUser)){
showPostNotification("" + pId, "" + pTitle, "" + pDescription);
}
}
else if (notificationType.equals("ChatNotification")){
//chat notification
String sent = remoteMessage.getData().get("sent");
String user = remoteMessage.getData().get("user");
FirebaseUser fUser = FirebaseAuth.getInstance().getCurrentUser();
if (fUser != null && sent.equals(fUser.getUid())) {
if (!savedCurrentUser.equals(user)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
sendOAndAboveNotification(remoteMessage);
} else {
sendNormalNotification(remoteMessage);
}
}
}
}
}
private void showPostNotification(String pId, String pTitle, String pDescription) {
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int notificationID = new Random().nextInt(3000);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
setupPostNotificationChannel(notificationManager);
}
//show post detail activity using post id when notification clicked
Intent intent = new Intent(this, ClickPostActivity.class);
intent.putExtra("PostKey", pId);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
//largeIcon
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.default_avatar);
//sound for notification
Uri notificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "" + ADMIN_CHANNEL_ID)
.setSmallIcon(R.drawable.default_avatar)
.setLargeIcon(largeIcon)
.setContentTitle(pTitle)
.setContentText(pDescription)
.setSound(notificationSoundUri)
.setContentIntent(pendingIntent);
//show notification
notificationManager.notify(notificationID, notificationBuilder.build());
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void setupPostNotificationChannel(NotificationManager notificationManager) {
CharSequence channelName = "New Notification";
String channelDescription = "Device to device post notification";
NotificationChannel adminChannel = new NotificationChannel(ADMIN_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH);
adminChannel.setDescription(channelDescription);
adminChannel.enableLights(true);
adminChannel.setLightColor(Color.RED);
adminChannel.enableVibration(true);
if (notificationManager!=null){
notificationManager.createNotificationChannel(adminChannel);
}
}
private void sendNormalNotification(RemoteMessage remoteMessage) {
String user = remoteMessage.getData().get("user");
String icon = remoteMessage.getData().get("icon");
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
RemoteMessage.Notification notification = remoteMessage.getNotification();
int i = Integer.parseInt(user.replaceAll("[\\D]", ""));
Intent intent = new Intent(this, ChatActivity.class);
Bundle bundle = new Bundle();
bundle.putString("visit_user_id", user);
intent.putExtras(bundle);
PendingIntent pIntent = PendingIntent.getActivity(this, i, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(Integer.parseInt(icon))
.setContentText(body)
.setContentTitle(title)
.setAutoCancel(true)
.setSound(defSoundUri)
.setContentIntent(pIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int j = 0;
if (i > 0) {
j = 1;
}
notificationManager.notify(j, builder.build());
}
@TargetApi(Build.VERSION_CODES.O)
private void sendOAndAboveNotification(RemoteMessage remoteMessage) {
String user = remoteMessage.getData().get("user");
String icon = remoteMessage.getData().get("icon");
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
RemoteMessage.Notification notification = remoteMessage.getNotification();
int i = Integer.parseInt(user.replaceAll("[\\D]", ""));
Intent intent = new Intent(this, ChatActivity.class);
Bundle bundle = new Bundle();
bundle.putString("visit_user_id", user);
intent.putExtras(bundle);
PendingIntent pIntent = PendingIntent.getActivity(this, i, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
OreoAndAboveNotification notification1 = new OreoAndAboveNotification(this);
Notification.Builder builder = notification1.getONotificatios(title, body, pIntent, defSoundUri, icon);
int j = 0;
if (i > 0) {
j = 1;
}
notification1.getManager().notify(j, builder.build());
}
@Override
public void onNewToken(@NonNull String s) {
super.onNewToken(s);
//update user yoken
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
//signed in, update token
updateToken(s);
}
}
private void updateToken(String tokenRefresh) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Tokens");
Token token = new Token(tokenRefresh);
ref.child(user.getUid()).setValue(token);
}
}
我需要上师的帮助:)或者任何帮助,来解决这个问题嘿,你解决过这个问题吗?就像格罗斯基在书中提到的那样忽略它
private static final String ADMIN_CHANNEL_ID = "admin_channel";
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
//get current user from shared preferences
SharedPreferences sp = getSharedPreferences("SP_USER", MODE_PRIVATE);
String savedCurrentUser = sp.getString("Current_USERID", "None");
/*we have two type notification
> notificationType ="PostNotification"
> notificationType ="ChatNotification"*/
String notificationType = remoteMessage.getData().get("notificationType");
if (notificationType.equals("PostNotification")){
//post notification
String sender = remoteMessage.getData().get("sent");
String pId = remoteMessage.getData().get("id");
String pTitle = remoteMessage.getData().get("name");
String pDescription = remoteMessage.getData().get("description");
//if user is same that has post don't show notification
if (!sender.equals(savedCurrentUser)){
showPostNotification("" + pId, "" + pTitle, "" + pDescription);
}
}
else if (notificationType.equals("ChatNotification")){
//chat notification
String sent = remoteMessage.getData().get("sent");
String user = remoteMessage.getData().get("user");
FirebaseUser fUser = FirebaseAuth.getInstance().getCurrentUser();
if (fUser != null && sent.equals(fUser.getUid())) {
if (!savedCurrentUser.equals(user)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
sendOAndAboveNotification(remoteMessage);
} else {
sendNormalNotification(remoteMessage);
}
}
}
}
}
private void showPostNotification(String pId, String pTitle, String pDescription) {
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
int notificationID = new Random().nextInt(3000);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
setupPostNotificationChannel(notificationManager);
}
//show post detail activity using post id when notification clicked
Intent intent = new Intent(this, ClickPostActivity.class);
intent.putExtra("PostKey", pId);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
//largeIcon
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.default_avatar);
//sound for notification
Uri notificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "" + ADMIN_CHANNEL_ID)
.setSmallIcon(R.drawable.default_avatar)
.setLargeIcon(largeIcon)
.setContentTitle(pTitle)
.setContentText(pDescription)
.setSound(notificationSoundUri)
.setContentIntent(pendingIntent);
//show notification
notificationManager.notify(notificationID, notificationBuilder.build());
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void setupPostNotificationChannel(NotificationManager notificationManager) {
CharSequence channelName = "New Notification";
String channelDescription = "Device to device post notification";
NotificationChannel adminChannel = new NotificationChannel(ADMIN_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_HIGH);
adminChannel.setDescription(channelDescription);
adminChannel.enableLights(true);
adminChannel.setLightColor(Color.RED);
adminChannel.enableVibration(true);
if (notificationManager!=null){
notificationManager.createNotificationChannel(adminChannel);
}
}
private void sendNormalNotification(RemoteMessage remoteMessage) {
String user = remoteMessage.getData().get("user");
String icon = remoteMessage.getData().get("icon");
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
RemoteMessage.Notification notification = remoteMessage.getNotification();
int i = Integer.parseInt(user.replaceAll("[\\D]", ""));
Intent intent = new Intent(this, ChatActivity.class);
Bundle bundle = new Bundle();
bundle.putString("visit_user_id", user);
intent.putExtras(bundle);
PendingIntent pIntent = PendingIntent.getActivity(this, i, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(Integer.parseInt(icon))
.setContentText(body)
.setContentTitle(title)
.setAutoCancel(true)
.setSound(defSoundUri)
.setContentIntent(pIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int j = 0;
if (i > 0) {
j = 1;
}
notificationManager.notify(j, builder.build());
}
@TargetApi(Build.VERSION_CODES.O)
private void sendOAndAboveNotification(RemoteMessage remoteMessage) {
String user = remoteMessage.getData().get("user");
String icon = remoteMessage.getData().get("icon");
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
RemoteMessage.Notification notification = remoteMessage.getNotification();
int i = Integer.parseInt(user.replaceAll("[\\D]", ""));
Intent intent = new Intent(this, ChatActivity.class);
Bundle bundle = new Bundle();
bundle.putString("visit_user_id", user);
intent.putExtras(bundle);
PendingIntent pIntent = PendingIntent.getActivity(this, i, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
OreoAndAboveNotification notification1 = new OreoAndAboveNotification(this);
Notification.Builder builder = notification1.getONotificatios(title, body, pIntent, defSoundUri, icon);
int j = 0;
if (i > 0) {
j = 1;
}
notification1.getManager().notify(j, builder.build());
}
@Override
public void onNewToken(@NonNull String s) {
super.onNewToken(s);
//update user yoken
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
//signed in, update token
updateToken(s);
}
}
private void updateToken(String tokenRefresh) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Tokens");
Token token = new Token(tokenRefresh);
ref.child(user.getUid()).setValue(token);
}