应用程序未显示Android 4.2.2中来自广播接收器的通知

时间:2019-02-20 18:26:07

标签: android notifications broadcastreceiver alarmmanager

我每天使用警报管理器和广播接收器显示通知,但这在某些设备上无法正常工作。

广播接收方中的通知代码:

when = System.currentTimeMillis();
cr = new Controller(context);
NotificationManager notificationManager = (NotificationManager) context
        .getSystemService(Context.NOTIFICATION_SERVICE);

Intent notificationIntent = new Intent(context, GetWordsActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
        notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

RemoteViews notificationLayout = new RemoteViews(context.getPackageName(), R.layout.notify_addwords);
RemoteViews notificationLayoutExpanded = new RemoteViews(context.getPackageName(), R.layout.notify_addwords);

NotificationCompat.Builder mNotifyBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(
                context).setSmallIcon(R.drawable.ic_morning)
                .setCustomContentView(notificationLayout)
                .setCustomBigContentView(notificationLayoutExpanded)
                .setAutoCancel(true).setWhen(when)
                .setOngoing(true)
                .setContentIntent(pendingIntent);
        notificationManager.notify(MID, mNotifyBuilder.build());

创建警报(来自MainActivity)

void nextDayCalendar()
{
    Calendar calendar = Calendar.getInstance();
    calendar.set(Calendar.HOUR_OF_DAY, 6);
    calendar.set(Calendar.MINUTE, 30);
    calendar.set(Calendar.SECOND, 0);
    if(calendar.getTime().compareTo(new Date()) < 0) calendar.add(Calendar.DAY_OF_MONTH, 1);
    Intent intent1 = new Intent(MainMenuActivity.this, NextDayCalendarReciever.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(MainMenuActivity.this, 0,intent1, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am = (AlarmManager) MainMenuActivity.this.getSystemService(MainMenuActivity.this.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}

广播接收者:

public class NextDayCalendarReciever extends BroadcastReceiver {
Context context;

@Override
public void onReceive(Context context, Intent intent) {
    // TODO Auto-generated method stub
    // Notification Code is here
    this.context = context;
}

清单:

<receiver android:name=".NextDayCalendarReciever" />

0 个答案:

没有答案