Android:警报无效

时间:2017-08-26 23:24:43

标签: android push-notification alarmmanager

我试图在未来3秒激活警报(用于测试目的),这会打开一条通知消息。它不起作用。

这是触发警报的按钮:

public void testButton() {


    AlarmManager alarmManager = (AlarmManager) getContext().getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(getContext(), AlertReceiver.class);
    PendingIntent alarmIntent = PendingIntent.getBroadcast(getContext(), 0, intent, 0);

    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 3000, alarmIntent);

}

这是AlertReceiver类。我是否理解正确,它应该在Intent启动时自动调用onReceive()?因为如果我手动调用该方法,通知会起作用(不是通过Intent而是通过该类的实例化对象)。 另外根据教程TaskStackBuilder.create(context)实际上应该传递一个活动而不是一个上下文,但我不能调用getActivity()。

public class AlertReceiver extends BroadcastReceiver {
private String mTitle = "test";
private String mText = "test text";


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

    NotificationUtils notificationUtils = new NotificationUtils(context);

    Notification.Builder nb = notificationUtils.getChannelNotification(mTitle, mText);

    Intent startApp = intent;

    TaskStackBuilder tStackBuilder = TaskStackBuilder.create(context);

    tStackBuilder.addParentStack(MainActivity.class);

    tStackBuilder.addNextIntent(startApp);

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

    nb.setContentIntent(pendingIntent);

    notificationUtils.getManager().notify(101, nb.build());
}

}

这是NotificationUtils类:

public class NotificationUtils extends ContextWrapper {

private NotificationManager mManager;
public static final String CHANNEL_ID = "task_reminder_channel";
public static final String CHANNEL_NAME = "Task Reminder Channel";

public NotificationUtils(Context base) {
    super(base);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        createChannels();
    }
}

@TargetApi(26)
public void createChannels() {
    NotificationChannel reminderChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
    reminderChannel.enableLights(true);
    reminderChannel.enableVibration(true);
    reminderChannel.setLightColor(R.color.colorPrimary);
    reminderChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

    getManager().createNotificationChannel(reminderChannel);
}

public NotificationManager getManager() {
    if (mManager == null) {
        mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }
    return mManager;
}

@TargetApi(26)
public Notification.Builder getChannelNotification(String title, String body) {

    return new Notification.Builder(getApplicationContext(), CHANNEL_ID)
            .setContentTitle(title)
            .setContentText(body)
            .setSmallIcon(R.drawable.ic_timelapse_primary)
            .setAutoCancel(true);
}

public NotificationCompat.Builder getChannelNotificationApiUnder26(String title, String body) {
    return new NotificationCompat.Builder(getApplicationContext())
            .setContentTitle(title)
            .setContentText(body)
            .setSmallIcon(R.drawable.ic_timelapse_primary)
            .setAutoCancel(true);
}

}

0 个答案:

没有答案