GithubHelp home page GithubHelp logo

Comments (3)

zengjingfang avatar zengjingfang commented on July 28, 2024
Error showing notification for service
java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)
at com.android.server.am.ServiceRecord$1.run(ServiceRecord.java:532)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.os.HandlerThread.run(HandlerThread.java:65)
at com.android.server.ServiceThread.run(ServiceThread.java:46)

普通的Notification

   notification = new Notification();
   startForeground(notificationId, notification);
Process: com.xtc.im.phonesample:push, PID: 29711
android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=null pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

设置channel的Notification

NotificationChannel channel = new NotificationChannel("id","name", NotificationManager.IMPORTANCE_LOW);
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            manager.createNotificationChannel(channel);
             notification = new Notification.Builder(this,"im_channel_id")
                     .setSmallIcon(R.drawable.ic_launcher)  // the status icon
                     .setWhen(System.currentTimeMillis())  // the time stamp
                     .setContentText("IM服务正在运行")  // the contents of the entry
                     .build();

我靠,是这个资源res/mipmap就这样,但是用drawable就好了,不过还不是指定的图片

Unable to load resource 0x7f030000 from pkg=com.xtc.im.phonesample
android.content.res.Resources$NotFoundException: Drawable com.xtc.im.phonesample:mipmap/ic_launcher with resource ID #0x7f030000
Caused by: android.content.res.Resources$NotFoundException: File res/mipmap-anydpi-v26/ic_launcher.xml from drawable resource ID #0x7f030000
at android.content.res.ResourcesImpl.loadDrawableForCookie(ResourcesImpl.java:820)
at android.content.res.ResourcesImpl.loadDrawable(ResourcesImpl.java:630)
at android.content.res.Resources.getDrawableForDensity(Resources.java:877)
at android.content.res.Resources.getDrawable(Resources.java:819)
at android.graphics.drawable.Icon.loadDrawableInner(Icon.java:324)
at android.graphics.drawable.Icon.loadDrawable(Icon.java:277)
at android.graphics.drawable.Icon.loadDrawableAsUser(Icon.java:388)

Process: com.xtc.im.phonesample:push, PID: 29379
android.app.RemoteServiceException: Bad notification posted from package com.xtc.im.phonesample: Couldn't create icon: StatusBarIcon(icon=Icon(typ=RESOURCE pkg=com.xtc.im.phonesample id=0x7f030000) visible user=0 )
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

from androidbox.

zengjingfang avatar zengjingfang commented on July 28, 2024

最后的姿势

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                context.startForegroundService(pushServiceIntent);
            }else {
                componentName = context.startService(pushServiceIntent);
            }

// service的onCreate
NotificationChannel channel = new NotificationChannel("im_channel_id","System", NotificationManager.IMPORTANCE_LOW);
            NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            manager.createNotificationChannel(channel);
             notification = new Notification.Builder(this,"im_channel_id")
                     .setSmallIcon(R.drawable.ic_launcher)  // the status icon
                     .setWhen(System.currentTimeMillis())  // the time stamp
                     .setContentText("IM服务正在运行")  // the contents of the entry
                     .build();

startForeground(notificationId, notification)

from androidbox.

SocratesR avatar SocratesR commented on July 28, 2024

what’s the type definition about "notificationId"?

from androidbox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.