通知不会出现在Android上

时间:2016-05-21 08:30:12

标签: android notifications calendar android-notifications

我有一个我从github抓起的日历库: https://github.com/prolificinteractive/material-calendarview

我让用户点击日期并为该日期添加提醒,然后会弹出一个警告对话框,并要求他们输入当天他们想要提醒的时间。

现在我能够将文本转换为简单格式,然后将其从日历对象中吐出到字符串中,因此日期和时间应该通过通知。但它似乎无论如何都不起作用

下面是设置警报的代码:

Calendar cal = Calendar.getInstance();
            AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            Intent notificationIntent = new Intent("android.media.action.DISPLAY_NOTIFICATION");
            notificationIntent.addCategory("android.intent.category.DEFAULT");
            PendingIntent broadcast = PendingIntent.getService(context, 100, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            try {
                cal.setTime(alertFormatter.parse(date));
                System.out.print("Date added successfully");
            } catch (ParseException e) {
                System.out.println("Failed to add date");
            }
            cal.add(Calendar.HOUR, Integer.parseInt(hour.getText().toString()));
            cal.add(Calendar.MINUTE, Integer.parseInt(minute.getText().toString()));
            cal.add(Calendar.SECOND, 0);
            if(spAMpm.getSelectedItem().equals("AM"))cal.add(Calendar.AM_PM, Calendar.AM);
            else if (spAMpm.getSelectedItem().equals("PM"))cal.add(Calendar.AM_PM, Calendar.PM);


            alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), broadcast);

然后我创建的接收器做了我需要做的事情:

public class UpcomingWorkNotification extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {

    Intent notificationIntent = new Intent(context, UpcomingWork.class);

    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
    stackBuilder.addParentStack(UpcomingWork.class);
    stackBuilder.addNextIntent(notificationIntent);

    PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Four.oh")
            .setContentText("Assignment Due Soon!")
            .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, builder.build());

}
}

在清单中我给了它这个权限并添加了接收器

<uses-permission android:name="android.permission.WAKE_LOCK"/>

 <receiver android:name=".UpcomingWorkNotification">
    <action android:name="android.media.action.DISPLAY_NOTIFICATION" />
    <category android:name="android.intent.category.DEFAULT" />
</receiver>

即使所有这些通知在我设置时仍然没有出现 有人可以帮忙吗? 谢谢你

0 个答案:

没有答案