简体   繁体   English

发送通知不起作用

[英]Sending notification does not work

I'm writing an app having minSdkVersion = 9 and maxSdkVersion = 21 . 我正在编写一个具有minSdkVersion = 9maxSdkVersion = 21的应用程序。 In service I have written a code to send notification, user will be notified when pre-set time matches current time. service我编写了一个发送通知的代码,当预先设定的时间与当前时间匹配时,将通知用户。

However, it notifies me perfectly if I run it in emulator having API = 19 but if I run in my device having API = 10 it does not notify me and crashes the app. 但是,如果我在API = 19的模拟器中运行它,它会完美地通知我,但如果我在我的设备中运行API = 10,它不会通知我并崩溃应用程序。

I'm using Android-Studio in Ubuntu, it doesn't provide device debugging feature. 我在Ubuntu中使用Android-Studio,它不提供设备调试功能。 So I can't even see the logcat . 所以我甚至看不到logcat

Here is my relevant code 这是我的相关代码

@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;
}

What is the issue? 有什么问题? Do I need to add any support library or something to make it work in lower API levels? 我是否需要添加任何支持库或其他东西才能使其在较低的API级别下工作?

The error is 错误是

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

My logcat while running in device having api 10 我在使用api 10的设备中运行时的logcat

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() ,以指示当用户点击Notification时应该发生什么。

I think I had that same Error. 我想我有同样的错误。 you need to put a PendingIntent in the setContentIntent() (like what the selected answer said) 你需要在setContentIntent()放置一个PendingIntent(就像所选答案所说的那样)

in your case like this : 在你的情况下像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM