【问题标题】:Sending notification does not work发送通知不起作用
【发布时间】:2015-02-26 19:58:08
【问题描述】:

我正在编写一个具有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.access$2800(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.access$2800(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)

【问题讨论】:

  • “它没有通知我并使应用程序崩溃”——然后查看 LogCat 中的 Java 堆栈跟踪。 “我在 Ubuntu 中使用 Android-Studio,它不提供设备调试功能”——在 Ubuntu 中使用 Android Studio,而且它肯定提供“提供设备调试功能”。
  • 你打开开发者选项了吗?在模拟器上运行您的应用程序并在您的 ide 中打开 Logcat 视图
  • @CommonsWare 在android sdk manager -> extras 类别中显示google usb drivernot compatible with linux。所以我不能直接在设备中运行。先生,如果 ubuntu 允许调试,您能告诉我该怎么做吗?
  • @KonradKrakowiak 是的,我在模拟器中运行应用程序,我可以在 logcat 中看到日志,但它没有显示错误。即使我在我的问题中发布。等一下。
  • 您不需要 OS X 或 Linux 的驱动程序。通常,您只需在开发人员选项中启用设备上的调试,然后将设备插入您的 Ubuntu 机器,它就可以工作了。这在the documentation 中有介绍。

标签: android service notifications android-notifications


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我想我有同样的错误。 您需要在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());
    

    【讨论】:

      猜你喜欢
      • 2019-05-28
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2018-05-02
      相关资源
      最近更新 更多