Java 单击通知时应用程序崩溃
我创建了一个包含聊天功能的应用程序,当用户发送消息时,它会向接收者显示一个通知 现在,我想当用户收到通知并单击它时,它将打开活动及其所有功能 我发出的通知如下所示:Java 单击通知时应用程序崩溃,java,android,firebase-cloud-messaging,android-notifications,android-pendingintent,Java,Android,Firebase Cloud Messaging,Android Notifications,Android Pendingintent,我创建了一个包含聊天功能的应用程序,当用户发送消息时,它会向接收者显示一个通知 现在,我想当用户收到通知并单击它时,它将打开活动及其所有功能 我发出的通知如下所示: public void notifyThis(String title, String message, String uniqueID, String var1, String var2, String var3) { Intent intent = new Intent(this, ChatActivity.c
public void notifyThis(String title, String message, String uniqueID, String var1, String var2, String var3) {
Intent intent = new Intent(this, ChatActivity.class);
intent.putExtra("ChatID",var1);
intent.putExtra("ReceiverID",var2);
intent.putExtra("SenderID",var3);
int RandomID = (int) System.currentTimeMillis();
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder groupBuilder = new NotificationCompat.Builder(this,"191919")
.setSmallIcon(R.drawable.ic_launcher_custom_background)
.setGroup(uniqueID)
.setGroupSummary(true);
NotificationCompat.Builder messageBuilder = new NotificationCompat.Builder(this, "191919")
.setSmallIcon(R.drawable.ic_launcher_custom_background)
.setContentTitle(title)
.setContentText(message)
.setContentIntent(pendingIntent)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setGroup( uniqueID )
.setAutoCancel( true );
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(uniqueID,0, groupBuilder.build());
notificationManager.notify(RandomID, messageBuilder.build());
}
到目前为止,一切正常,当我调试时,我看到var1、var2、var3
不是空的并且存在
我的问题是,当我点击通知时,应用程序崩溃,因为它说:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
但是我不明白,如果通知的目的是用值发送的,并且我是这样读的,为什么SenderID会变为null
protected void onCreate(Bundle savedInstanceState) {
super.onCreate( savedInstanceState );
setContentView( R.layout.activity_chat );
getSupportActionBar().hide();
auth = FirebaseAuth.getInstance();
chatID = getIntent().getExtras().getString( "ChatID" );
receiverID = getIntent().getExtras().getString( "ReceiverID" );
SenderID = getIntent().getExtras().getString("SenderID");
if (SenderID.equals( auth.getUid() )) { HERE I GET THE ERROR SAYING SenderID IS NULL
NotMyID = receiverID;
} else {
NotMyID = SenderID;
}
}
我读错什么了吗?据我所知,我在通知意图中发送SenderID,然后我根据该意图getExtras
,它应该可以工作
谢谢只需对notify函数进行一些更改:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
您好,您通过intent传递的其他变量是否可用?试着把帐篷搭好。将_CANCEL_CURRENT标记为pendingent.getActivity的最后一个参数。希望您早点来。非常感谢!唯一的问题是,如果用户有超过1个通知(比如说来自2个用户),那么它只会对最后一个通知生效,其他用户不会打开任何通知,因此需要将requestCode更改为特定ID=]