简体   繁体   English

单击通知时应用程序崩溃

[英]App crashes when clicking on notification

I created an app that has chat in it and when users send messages it shows a notification to the receiver.我创建了一个包含聊天功能的应用程序,当用户发送消息时,它会向接收者显示通知。

Now, I had like that when the user gets a notification and clicks on it, it will open the activity with all of its functionality.现在,我喜欢当用户收到通知并单击它时,它将打开具有所有功能的活动。

The notification that I make looks like this:我发出的通知如下所示:

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());

    }

So far everything works ok and when I debug I see that var1, var2, var3 are not null and exist.到目前为止一切正常,当我调试时,我看到var1, var2, var3不是 null 并且存在。

My problem is that when I click on the notification, the app crashes since it says:我的问题是,当我点击通知时,应用程序崩溃了,因为它说:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference

But I can't understand why it gets SenderID to be null if the intent of the notification is sent with value in it and if I read it like this;但是我不明白为什么如果通知的意图是在其中发送带有值的并且我这样阅读它,为什么它会得到 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;
        }
    }

Am I reading something wrong?我读错了吗? From my understanding I send the SenderID in my notification intent and then I getExtras from that intent and it should work.据我了解,我在通知意图中发送了getExtras ,然后我从该意图中获取了额外的内容,它应该可以工作。

Thank you谢谢

Just do some changes to the notifyThis function:只需对 notifyThis function 做一些更改:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM