美文网首页
Android8.0收不到通知的问题解决方法

Android8.0收不到通知的问题解决方法

作者: 慢牛策略 | 来源:发表于2018-10-18 15:14 被阅读42次
Android8.0收不到通知,修改后的代码如下:
 public static void startBackgroundNotification(TimeLineModel model) {
        NotificationManager mNotificationManager = (NotificationManager) App.getContext().getSystemService(Context.NOTIFICATION_SERVICE);

        String mChannelId = "channelID";
        //TODO 重点1 
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = App.getContext().getString(R.string.app_name);
            NotificationChannel channel = new NotificationChannel(mChannelId, name, NotificationManager.IMPORTANCE_DEFAULT);
            mNotificationManager.createNotificationChannel(channel);
        }


        int flag = model.getId();
        Intent bIntent = new Intent(App.getContext(), BlackBackgroundActivity.class);
        bIntent.putExtra("todo_info", model);
        String title = "";
        if (model != null && model.getData_info() != null) {
            title = model.getData_info().getTitle();
        }

        PendingIntent pbIntent = PendingIntent.getActivity(App.getContext(), flag, bIntent, PendingIntent.FLAG_ONE_SHOT);
         //TODO 重点2:此处需要保证使用最新的版本,
        NotificationCompat.Builder bBuilder =
                new NotificationCompat.Builder(App.getContext(), mChannelId)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setContentTitle(App.getContext().getResources().getString(R.string.app_name))
                        .setContentText(title)
                        .setAutoCancel(true)
                        .setContentIntent(pbIntent);
        Notification notification = bBuilder.build();
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        mNotificationManager.notify(flag, notification);
    }

遇到的问题:

1 Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
解决方法:  distributionUrl=http\://services.gradle.org/distributions/gradle-4.1-all.zip
https换成http即可

2 Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com/r/tools/flavorDimensions-missing-error-message.html

 解决方法:
在model目录的build.gradle中添加:        
    flavorDimensions "1.0"

3 Error:Unable to resolve dependency for ':app@debug/compileClasspath': Could not resolve com.android.support.constraint:constraint-layout:1.1.3.

升级所有tool及sdk版本

关于Android O 通知渠道总结

相关文章

网友评论

      本文标题:Android8.0收不到通知的问题解决方法

      本文链接:https://www.haomeiwen.com/subject/nufszftx.html