发送通知不起作用

Sending notification does not work

我正在编写一个具有 minSdkVersion = 9maxSdkVersion = 21 的应用程序。在service我写了一个发送通知的代码,当预设时间与当前时间匹配时,用户会收到通知。

但是,如果我在具有 API = 19 的模拟器中 运行 它会完美地通知我,但是如果我在我的设备中 运行 API = 10 它不会通知我并使应用程序崩溃。

我在 Ubuntu 中使用 Android-Studio,它不提供设备调试功能。所以我什至看不到 logcat.

这是我的相关代码

@Override
public int onStartCommand(Intent intent, int flags, int startId){

// Intent alarmIntent = new Intent(getBaseContext(), TRAlarmScreen.class);
//    alarmIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//    alarmIntent.putExtras(intent);
//    getApplication().startActivity(alarmIntent); */

    Bundle extras = intent.getExtras();
    int id = extras.getInt("id");
    String title = extras.getString("title");
    String des = extras.getString("des");

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
    mBuilder.setContentTitle(title);
    mBuilder.setContentText(des);
    mBuilder.setTicker("New Message Alert!");
    mBuilder.setSmallIcon(R.drawable.tr);

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(id, mBuilder.build());

    return START_STICKY;
}

这是什么问题?我是否需要添加任何支持库或其他东西以使其在较低的 API 级别工作?

错误是

java.lang.IllegalArgumentException: contentIntent required: pkg=com.android.saathi id=1 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x0)

我的 logcat 而 运行 在具有 api 10

的设备中
02-27 02:27:41.812    4760-4760/com.android.saathi E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start service com.android.saathi.TRService@405a3bd0 with Intent { flg=0x4 cmp=com.android.saathi/.TRService (has extras) }: java.lang.IllegalArgumentException: contentIntent required: pkg=com.android.saathi id=1 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x0)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2372)
        at android.app.ActivityThread.access00(ActivityThread.java:132)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1111)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:150)
        at android.app.ActivityThread.main(ActivityThread.java:4277)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalArgumentException: contentIntent required: pkg=com.android.saathi id=1 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x0)
        at android.os.Parcel.readException(Parcel.java:1326)
        at android.os.Parcel.readException(Parcel.java:1276)
        at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:394)
        at android.app.NotificationManager.notify(NotificationManager.java:111)
        at android.app.NotificationManager.notify(NotificationManager.java:91)
        at com.android.saathi.TRService.onStartCommand(TRService.java:42)
        at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2355)
        at android.app.ActivityThread.access00(ActivityThread.java:132)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1111)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:150)
        at android.app.ActivityThread.main(ActivityThread.java:4277)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)

在您的 NotificationCompat.Builder 上调用 setContentIntent(),以指示当用户点击 Notification 时应该发生什么。

我想我也有同样的错误。 您需要在 setContentIntent() 中放置一个 PendingIntent(就像所选答案所说的那样)

你的情况是这样的:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
  mBuilder.setContentTitle(title);
  mBuilder.setContentText(des);
  mBuilder.setTicker("New Message Alert!");
  mBuilder.setSmallIcon(R.drawable.tr);

PendingIntent resultPendingIntent =
            PendingIntent.getActivity(
                    this,
                    0,
                    resultIntent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
    mBuilder.setContentIntent(resultPendingIntent);


NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(id, mBuilder.build());