点击我的通知

时间:2015-10-28 10:25:54

标签: android android-intent android-notifications android-pendingintent

我制作了一个应用程序,其中包含一个由警报管理员触发的通知,只有该通知无法点击。我该怎么做?

MainActivity的代码:

private void scheduleNotification(Notification notification, int delay) {

    Intent notificationIntent = new Intent(this, NotificationPublisher.class);
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION_ID, 1);
    notificationIntent.putExtra(NotificationPublisher.NOTIFICATION, notification);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    long futureInMillis = SystemClock.elapsedRealtime() + delay;
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, futureInMillis, pendingIntent);
}

    private Notification getNotification(String titel, String content) {
    Notification.Builder builder = new Notification.Builder(this);
    builder.setContentTitle(titel);
    builder.setContentText(content);
    builder.setSmallIcon(R.mipmap.ic_launcher);
    return builder.build();
}
来自我的通知发布类的

代码:

public class NotificationPublisher extends BroadcastReceiver {

public static String NOTIFICATION_ID = "notification-id";
public static String NOTIFICATION = "notification";

public void onReceive(Context context, Intent intent)
{

    NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = intent.getParcelableExtra(NOTIFICATION);
    int id = intent.getIntExtra(NOTIFICATION_ID, 0);
    notificationManager.notify(id, notification);

}

}

1 个答案:

答案 0 :(得分:3)

您可能正在寻找方法

setContentIntent(PROVIDE_YOUR_PENDING_INTENT);

您需要在点击通知时向用户重定向的位置提供待处理的意图。

参考:http://developer.android.com/reference/android/app/Notification.Builder.html#setContentIntent(android.app.PendingIntent)