默认
发表评论 4
想开发IM:买成品怕坑?租第3方怕贵?找开源自已撸?尽量别走弯路了... 找站长给点建议
[已回复] android9.0中通知机制API的变更,导致的NotificationHelper中崩溃的问题
阅读(29967) | 评论(4 收藏 淘帖
E/Notification: setLatestEventInfo() is deprecated and you should feel deprecated.
    java.lang.Throwable
        at android.app.Notification.setLatestEventInfo(Notification.java:2829)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.eva.android.NotificationHelper$NotificationCreator.buildNotificationPreHoneycomb(NotificationHelper.java:169)
        at com.eva.android.NotificationHelper$NotificationCreator.createNotification(NotificationHelper.java:145)
        at com.x52im.rainbowchat.service.GeniusService.showNotification(GeniusService.java:122)
        at com.x52im.rainbowchat.service.GeniusService.onCreate(GeniusService.java:54)
        at android.app.ActivityThread.handleCreateService(ActivityThread.java:3565)
        at android.app.ActivityThread.access$1400(ActivityThread.java:200)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1689)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:201)
        at android.app.ActivityThread.main(ActivityThread.java:6806)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)



报错原因是由于NotificationHelper类的此方法,反射调用了setLatestEventInfo()方法,此方法在最新的sdk中此方法已被移除,不存在,导致程序崩溃

@SuppressWarnings("deprecation")
private static Notification buildNotificationPreHoneycomb(Context context, PendingIntent pendingIntent
        , String title, String text, int iconId)
{
    Notification notification = new Notification(iconId, "", System.currentTimeMillis());
    try {
        // try to call "setLatestEventInfo" if available
        Method m = notification.getClass().getMethod("setLatestEventInfo"
                , Context.class, CharSequence.class, CharSequence.class, PendingIntent.class);
        m.invoke(notification, context, title, text, pendingIntent);
    } catch (Exception e) {
        // do nothing
    }
    return notification;
}

即时通讯网 - 即时通讯开发者社区! 来源: - 即时通讯开发者社区!

上一篇:[已回复] ios RainbowChat仿微信长按气泡框弹出收藏按钮怎么做?下一篇:[已回复] 关于android版升级到最新targetSDK=28后,再提两个兼容性的崩溃bug
推荐方案
评论 4
android的这个通知机制,到android 9.0已经是第3次大改动了,兼容性跟屎一样,相当无聊。

你目前的targetSDK设置的是多少?
28
经测试,是由于NotificationHelper类中,isNotificationBuilderSupported()方法中的Class.forName("android.app.Notification.Builder") != null这一句报错导致创建的Notification为低版本的
public static boolean isNotificationBuilderSupported() {
            try {
                return (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                        && Class.forName("android.app.Notification.Builder") != null;
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
                return false;
            }
        }
引用:行长 发表于 2019-02-20 10:53
经测试,是由于NotificationHelper类中,isNotificationBuilderSupported()方法中的Class.forName("android ...

明白。目前RainbowChat还没有来的及将targetSDK版本支持到28,接下来的版本里会考虑升级到28。原版工程里,因targetSDK没有到28,所以不会出现帖子里说的兼容问题。
你如果还有其它的建议,可以随时论坛里反馈给我,我会记录下来,以后新版里处理并合并到主分支。

多谢反馈。

打赏楼主 ×
使用微信打赏! 使用支付宝打赏!

返回顶部