简体   繁体   English

Android 通知未显示

[英]Android notifications not showing

I'm trying to make a daily notification which will be show at a specific time.我正在尝试制作将在特定时间显示的每日通知。 Unfortunately it doesn't show.不幸的是它没有显示。

I tried to follow couples tuto (also from developer.android.com ) and checked similar questions that have already been asked.我试图关注夫妇 tuto(也来自developer.android.com )并检查了已经被问到的类似问题。 To save hour I'm using Hawk library.为了节省时间,我正在使用 Hawk 库。

Intent intent = new Intent(getContext(), AlarmReceiver.class);
    int notificationId = 1;
    intent.putExtra("notificationId", notificationId);

PendingIntent alarmIntent = PendingIntent.getBroadcast(getContext(), 0,
            intent,PendingIntent.FLAG_NO_CREATE);

AlarmManager alarm = (AlarmManager)  getContext().getSystemService(getContext().ALARM_SERVICE);


    switch (view.getId()) {
    int hour = timePicker.getCurrentHour();
            int minute = timePicker.getCurrentMinute();

            // Create time
            ....

            //set alarm
            alarm.setRepeating(AlarmManager.RTC_WAKEUP, alarmStartTime, AlarmManager.INTERVAL_DAY, alarmIntent);

            Hawk.put("notification_hour", alarmStartTime);

            break;

        case R.id.cancel_button:
//cancel notification
            break;
    }
}

and here AlarmReceiver class这里是 AlarmReceiver 类

public class AlarmReceiver extends BroadcastReceiver {

    public AlarmReceiver () {
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        sendNotification(context, intent);
    }

    private void sendNotification(Context con, Intent intent) {

        int notificationId = intent . getIntExtra ("notificationId", 1);
        String message = " message";

        Intent mainIntent = new Intent(con, MainActivity.class);
        PendingIntent contentIntent = PendingIntent . getActivity (con, 0, mainIntent, 0);

        NotificationManager myNotificationManager =(NotificationManager) con . getSystemService (Context.NOTIFICATION_SERVICE);

        Notification.Builder builder = new Notification.Builder(con);
        builder.setSmallIcon(android.R.drawable.ic_dialog_info)
            .setContentTitle("Reminder")
            .setContentText(message)
            .setWhen(System.currentTimeMillis())
            .setAutoCancel(true)
            .setContentIntent(contentIntent)
            .setPriority(Notification.PRIORITY_MAX)
            .setDefaults(Notification.DEFAULT_ALL);

        myNotificationManager.notify(notificationId, builder.build());
    }
}

In OREO, they have redesigned notifications to provide an easier and more consistent way to manage notification behavior and settings.在 OREO 中,他们重新设计了通知,以提供一种更简单、更一致的方式来管理通知行为和设置。 Some of these changes include:其中一些变化包括:

Notification channels : Android 8.0 introduces notification channels that allow you to create a user-customizable channel for each type of notification you want to display.通知渠道:Android 8.0 引入了通知渠道,允许您为要显示的每种通知类型创建用户可自定义的渠道。 Notification dots: Android 8.0 introduces support for displaying dots, or badges, on app launcher icons.通知点:Android 8.0 支持在应用启动器图标上显示点或徽章。 Notification dots reflect the presence of notifications that the user has not yet dismissed or acted on.通知点反映了用户尚未解除或采取行动的通知的存在。

Snoozing : Users can snooze notifications, which causes them to disappear for a period of time before reappearing.打盹:用户可以暂停通知,这使他们消失重现之前的一段时间。 Notifications reappear with the same level of importance they first appeared with.通知重新出现时的重要性与它们首次出现时相同。

Messaging style : In Android 8.0, notifications that use the MessagingStyle class display more content in their collapsed form.消息样式:在 Android 8.0 中,使用 MessagingStyle 类的通知以折叠形式显示更多内容。 You should use theMessagingStyle class for notifications that are messaging-related.对于与消息相关的通知,您应该使用 MessagingStyle 类。 Here, we have created the NotificationHelper class that require the Context as the constructor params.在这里,我们创建了需要 Context 作为构造函数参数的 NotificationHelper 类。 NOTIFICATION_CHANNEL_ID variable has been initialize in order to set the channel_id to NotificationChannel. NOTIFICATION_CHANNEL_ID 变量已初始化,以便将 channel_id 设置为 NotificationChannel。

The method createNotification(…) requires title and message parameters in order to set the title and content text of the notification.方法createNotification(...)需要标题和消息参数,以便设置通知的标题和内容文本。 In order to handle the notification click event we have created the pendingIntent object, that redirect towards SomeOtherActivity.class.为了处理通知点击事件,我们创建了 pendingIntent 对象,该对象重定向到 SomeOtherActivity.class。

Notification channels allow you to create a user-customizable channel for each type of notification you want to display.通知渠道允许您为要显示的每种通知类型创建用户可自定义的渠道。 So, if the android version is greater or equals to 8.0, we have to create the NotificationChannel object and set it to createNotificationChannel(…) setter property of NotificationManager.所以,如果android版本大于或等于8.0,我们必须创建NotificationChannel对象并将其设置为NotificationManager的createNotificationChannel(…) setter属性。

public class NotificationHelper {

private Context mContext;
private NotificationManager mNotificationManager;
private NotificationCompat.Builder mBuilder;
public static final String NOTIFICATION_CHANNEL_ID = "10001";

public NotificationHelper(Context context) {
    mContext = context;
}

/**
 * Create and push the notification 
 */
public void createNotification(String title, String message)
{    
    /**Creates an explicit intent for an Activity in your app**/
    Intent resultIntent = new Intent(mContext , SomeOtherActivity.class);
    resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(mContext,
            0 /* Request code */, resultIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);

    mBuilder = new NotificationCompat.Builder(mContext);
    mBuilder.setSmallIcon(R.mipmap.ic_launcher);
    mBuilder.setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(false)
            .setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
            .setContentIntent(resultPendingIntent);

    mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O)
    {
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", importance);
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.enableVibration(true);
        notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        assert mNotificationManager != null;
        mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID);
        mNotificationManager.createNotificationChannel(notificationChannel);
    }
    assert mNotificationManager != null;
    mNotificationManager.notify(0 /* Request Code */, mBuilder.build());
}

Just include a NotificationChannel and set a channel id to it.只需包含一个 NotificationChannel 并为其设置一个频道 ID。

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

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