【问题标题】:App crashes when clicking on notification单击通知时应用程序崩溃
【发布时间】:2020-06-04 19:17:13
【问题描述】:

我创建了一个包含聊天功能的应用,当用户发送消息时,它会向接收者显示通知。

现在,我喜欢当用户收到通知并点击它时,它会打开具有所有功能的活动。

我发出的通知如下所示:

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 为空;

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,它应该可以工作。

谢谢

【问题讨论】:

  • 您好,您通过意图传递的其他变量是否可用?尝试设置 PendingIntent。 FLAG_CANCEL_CURRENT 作为 PendingIntent.getActivity 的最后一个参数。
  • 希望你早点来。非常感谢!
  • 唯一的是,如果用户有超过 1 个通知(假设来自 2 个用户)它只会对最后一个通知有效,其他通知不会打开任何东西,因此需要更改 requestCode到特定的 ID =]

标签: java android firebase-cloud-messaging android-notifications android-pendingintent


【解决方案1】:

只需对 notifyThis 函数做一些更改:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    • 1970-01-01
    • 2017-09-27
    • 2021-11-20
    相关资源
    最近更新 更多