1

我想从我的应用程序中设置提醒。设置好时间后,我希望能够在设置的时间到来时收到提醒。我已设置通知,我正在尝试设置通知渠道,但没有收到任何通知。

这是我的代码:

protected void onHandleIntent(Intent intent) {
    createNotificationChannel();
    mTodoText = intent.getStringExtra(TODOTEXT);
    mTodoUUID = (UUID) intent.getSerializableExtra(TODOUUID);
    Intent i = new Intent(this, ReminderActivity.class);
    i.putExtra(TodoNotificationService.TODOUUID, mTodoUUID);
    Intent deleteIntent = new Intent(this, DeleteNotificationService.class);
    deleteIntent.putExtra(TODOUUID, mTodoUUID);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id)).setContentTitle("Reminder").setContentText(mTodoText).setDeleteIntent(PendingIntent.getService(this, mTodoUUID.hashCode(), deleteIntent, PendingIntent.FLAG_UPDATE_CURRENT)).setContentIntent(PendingIntent.getActivity(this, mTodoUUID.hashCode(), i, PendingIntent.FLAG_UPDATE_CURRENT)).setPriority(NotificationCompat.PRIORITY_DEFAULT);
    int mNotificationId = (int) System.currentTimeMillis();
    NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    mNotifyMgr.notify(mNotificationId, builder.build());
}

private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "Reminders Notifications";
        String desc = "Stores reminders";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;

        NotificationChannel notificationChannel = new NotificationChannel(getString(R.string.default_notification_channel_id), name, importance);
        notificationChannel.setDescription(desc);

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
    }
}
4

0 回答 0