android - 使我的通知可点击

标签 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();
}

来 self 的通知发布类的代码:

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);

}

最佳答案

你可能正在寻找方法

setContentIntent(PROVIDE_YOUR_PENDING_INTENT);

当用户点击通知时,您需要提供一个待定 Intent ,指向您希望将用户重定向到的位置。

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

关于android - 使我的通知可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33388522/

相关文章:

android - 在屏幕设计方面需要帮助

android - 第二次通知无效

android Lollipop 通知背景颜色

android - 可穿戴设备上未显示通知操作图标

java - 设备上的 Webview 应用程序分辨率太大

android - 如何在android中创建条纹进度条

android - 无法在 Android Studio 项目中找到生成的应用程序 .apk

Android:服务类中的 intent.putExtra 以获取通知

android - 为什么要使用 Intent.URI_INTENT_SCHEME

Android 多重通知避免更新时闪烁