通知未出现(通知频道)

时间:2019-09-20 12:28:05

标签: java android push-notification notification-channel

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

这是我的代码:

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

0 个答案:

没有答案