单头通知按钮不执行

时间:2016-12-25 09:29:29

标签: android android-studio

我一直在寻找几个小时,但无法找到解决问题的方法。有谁知道如何让抬头通知按钮调用广播?我的代码:

警报接收器通知生成器:

       NotificationCompat.Builder builder =
            new NotificationCompat.Builder(context)
                    .setSmallIcon(R.drawable.alarmicon)
                    .setContentTitle("Alarm for " + timeString)
                    .setContentText(MainActivity.alarmLabel.getText().toString())
                    .setDefaults(Notification.DEFAULT_ALL) // must requires VIBRATE permission
                    .setPriority(NotificationCompat.PRIORITY_HIGH); //must give priority to High, Max which will considered as heads-up notification

    //set intents and pending intents to call service on click of "dismiss" action button of notification
    Intent dismissIntent = new Intent(context, notificationButtonAction.class);
    dismissIntent.setAction(DISMISS_ACTION);
    PendingIntent piDismiss = PendingIntent.getBroadcast(context, 0, dismissIntent, 0);
    builder.addAction(R.drawable.alarmoff, "Dismiss", piDismiss);

    //set intents and pending intents to call service on click of "snooze" action button of notification
    Intent snoozeIntent = new Intent(context, notificationButtonAction.class);
    snoozeIntent.setAction(SNOOZE_ACTION);
    PendingIntent piSnooze = PendingIntent.getBroadcast(context, 0, snoozeIntent, 0);
    builder.addAction(R.drawable.snooze, "Snooze", piSnooze);

    // Gets an instance of the NotificationManager service
    NotificationManager notificationManager =
            (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    //to post your notification to the notification bar with a id. If a notification with same id already exists, it will get replaced with updated information.
    notificationManager.notify(0, builder.build());

notificationButtonAction:

public static class notificationButtonAction extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        System.out.println("notificationButtonAction Started");
        String action = intent.getAction();
        if (SNOOZE_ACTION.equals(action)) {
            stopAlarm();
            System.out.println("Alarm Snoozed");
            MainActivity ma = new MainActivity();
            ma.setAlarm(true);
        }
        else if (DISMISS_ACTION.equals(action)) {
            stopAlarm();
            System.out.println("Alarm Dismissed");
        }
    }
}

我在notificationButtonAction中的打印行不打印,甚至没有“notificationButtonAction Started”。

我遵循了Brevity Software(http://www.brevitysoftware.com/blog/how-to-get-heads-up-notifications-in-android/)的教程,但他们的代码似乎没有用。

任何帮助表示赞赏!谢谢!

1 个答案:

答案 0 :(得分:0)

原来,我没有将类添加到清单中。我的代码很好。