Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 发送通知不起作用_Android_Service_Notifications_Android Notifications - Fatal编程技术网

Android 发送通知不起作用

Android 发送通知不起作用,android,service,notifications,android-notifications,Android,Service,Notifications,Android Notifications,我正在编写一个应用程序,它有minSdkVersion=9和maxSdkVersion=21。在服务中我编写了一个发送通知的代码,当预设时间与当前时间匹配时,将通知用户 但是,如果我在具有API=19的emulator中运行它,它会完美地通知我,但如果我在具有API=10的设备中运行它,它不会通知我并使应用程序崩溃 我在Ubuntu中使用Android Studio,它不提供设备调试功能。所以我甚至看不到logcat 这是我的相关代码 @Override public int onStartCo

我正在编写一个应用程序,它有
minSdkVersion=9
maxSdkVersion=21
。在
服务中
我编写了一个发送通知的代码,当预设时间与当前时间匹配时,将通知用户


但是,如果我在具有API=19的emulator中运行它,它会完美地通知我,但如果我在具有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必需:pkg=com.android.saathi id=1通知=notification(振动=null,声音=null,默认值=0x0,标志=0x0)

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

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

,我想我也有同样的错误。 您需要在
setContentIntent()
中放置一个PendingEvent(如所选答案所示)

在您这样的情况下:

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

“它不会通知我并使应用程序崩溃”——然后查看LogCat中的Java堆栈跟踪。“我在Ubuntu中使用的是Android Studio,它不提供设备调试功能”——我在Ubuntu中使用的是Android Studio,它肯定“提供设备调试功能”。您是否打开开发人员选项?在emulator上运行您的应用程序,并在浏览器中打开Logcat视图ide@CommonsWare在
androidsdkmanager->extras
类别中,它显示
googleusb驱动程序
与linux不兼容。所以我不能直接在设备中运行。先生,如果ubuntu允许调试,你能告诉我怎么做吗?@KonradKrakowiak是的,我在emulator中运行应用程序,我可以在logcat中看到日志,但它没有显示任何错误。即使我在我的问题中发帖。请稍等。您不需要OS X或Linux的驱动程序。通常,您只需在开发者选项中启用设备调试,并将设备插入Ubuntu机器,即可正常工作。这是一本书。