通知不适用于Android Oreo

时间:2019-01-01 17:36:01

标签: android notifications

我在我的应用中使用了本地通知。用户每天都会收到通知。在7版以下的版本中,它工作正常,而在8.0版中则不能。

为什么正常通知生成器在Android O上不显示通知。

如何在Android 8 Oreo上显示通知?

是否需要添加新的代码以在Android O上显示通知?

这是我的Notification Service代码,该代码在android 7上有效,但在android 8上无效

public class NotificationService extends Service {
    private static final String NOTIFICATION_DELETED_ACTION = "NOTIFICATION_DELETED";
    boolean canceledNotification = false;
    int imageId;
    int largeImageId;
    String lastCancelDate = "";
    Methods methods = new Methods();
    int notifyID = 1000;
    private final BroadcastReceiver receiver = new C03671();
    String title;
    CalendarTool tool = new CalendarTool();
    int updateMinutes = 0;



        public void onReceive(Context context, Intent intent) {
            Editor editor = PreferenceManager.getDefaultSharedPreferences(context).edit();
            editor.putBoolean("CANCELED_NOTIFICATION", true);
            editor.putString("CANCELED_DATE", NotificationService.this.methods.calendarToString(Calendar.getInstance(), Methods.DATEFORMAT));
            editor.apply();
            NotificationService.this.unregisterReceiver(this);
        }
    }

    public IBinder onBind(Intent intent) {
        return null;
    }

    @SuppressLint("WrongConstant")
    public int onStartCommand(Intent intent, int flags, int startId) {
        this.tool = new CalendarTool();
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
        boolean showNotification = sharedPref.getBoolean("SHOW_NOTIFICATION", true);
        this.canceledNotification = sharedPref.getBoolean("CANCELED_NOTIFICATION", false);
        this.lastCancelDate = sharedPref.getString("CANCELED_DATE", "");
        this.updateMinutes = sharedPref.getInt("UPDATE_NOTIFICATION_MIN", 0);
        if (showNotification) {
            Bitmap bitmap;
            this.title = this.methods.ReplaceNumber(this.tool.getJalaliStringLong());
            this.imageId = getResources().getIdentifier("d" + String.valueOf(this.tool.getIranianDay()), "drawable", getPackageName());
            this.largeImageId = getResources().getIdentifier("date" + String.valueOf(this.tool.getIranianDay()), "drawable", getPackageName());
            if (VERSION.SDK_INT < 24) {
                bitmap = this.methods.getFontBitmap(this, this.methods.ReplaceNumber(String.valueOf(this.tool.getIranianDay())), ContextCompat.getColor(this, R.color.colorPrimary), 50, "nazanin.ttf");
            } else {
                bitmap = BitmapFactory.decodeResource(getResources(), this.largeImageId);
            }
            int[] attrs = new int[]{16842904};
            TypedArray titleStyle = obtainStyledAttributes(R.style.NotificationTitle, attrs);
            TypedArray textStyle = obtainStyledAttributes(R.style.NotificationText, attrs);
            int titleColor = titleStyle.getColor(0, ViewCompat.MEASURED_STATE_MASK);
            int textColor = textStyle.getColor(0, -7829368);
            RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.notification_layout);
            contentView.setImageViewBitmap(R.id.notifImage, bitmap);
            contentView.setImageViewBitmap(R.id.notifTitle, this.methods.getFontBitmap(this, this.title, titleColor, 16, "iranyekanregular.ttf"));
            contentView.setImageViewBitmap(R.id.notifGregText, this.methods.getFontBitmap(this, this.methods.ReplaceNumber(this.tool.getGregorianStringFarsiShort()), textColor, 14, "iranyekanregular.ttf"));
            contentView.setImageViewBitmap(R.id.notifLunarText, this.methods.getFontBitmap(this, this.methods.ReplaceNumber(this.tool.getLunarStringShort()), textColor, 14, "iranyekanregular.ttf"));
            contentView.setImageViewBitmap(R.id.notifSpaceText, this.methods.getFontBitmap(this, " - ", textColor, 14, "iranyekanregular.ttf"));
            titleStyle.recycle();
            textStyle.recycle();
            PendingIntent deletePendIntent;
            NotificationManager mNotificationManager;
            Intent intent2;
            Builder mBuilder;
            if (this.canceledNotification) {
                Date lastDate = this.methods.StringDateToDate(this.lastCancelDate, Methods.DATEFORMAT);
                Calendar lastCalendar = Calendar.getInstance();
                lastCalendar.setTime(lastDate);
                Calendar nowCalendar = Calendar.getInstance();
                lastCalendar.add(12, this.updateMinutes);
                if (nowCalendar.getTime().after(lastCalendar.getTime())) {
                    deletePendIntent = PendingIntent.getBroadcast(this, 0, new Intent(NOTIFICATION_DELETED_ACTION), 0);
                    registerReceiver(this.receiver, new IntentFilter(NOTIFICATION_DELETED_ACTION));
                    mNotificationManager = (NotificationManager) getSystemService("notification");
                    intent2 = new Intent(this, MainActivity.class);
                    intent2.setFlags(603979776);
                    mBuilder = new Builder(this).setSmallIcon(this.imageId).setContent(contentView).setDeleteIntent(deletePendIntent).setContentIntent(PendingIntent.getActivity(this, 0, intent2, 0));
                    if (this.updateMinutes == 0) {
                        mBuilder.setAutoCancel(false).setOngoing(true);
                    } else {
                        mBuilder.setAutoCancel(true);
                    }
                    mNotificationManager.notify(this.notifyID, mBuilder.build());
                }
            } else {
                deletePendIntent = PendingIntent.getBroadcast(this, 0, new Intent(NOTIFICATION_DELETED_ACTION), 0);
                registerReceiver(this.receiver, new IntentFilter(NOTIFICATION_DELETED_ACTION));
                mNotificationManager = (NotificationManager) getSystemService("notification");
                intent2 = new Intent(this, MainActivity.class);
                intent2.setFlags(603979776);
                mBuilder = new Builder(this).setSmallIcon(this.imageId).setContent(contentView).setDeleteIntent(deletePendIntent).setContentIntent(PendingIntent.getActivity(this, 0, intent2, 0));
                if (this.updateMinutes == 0) {
                    mBuilder.setAutoCancel(false).setOngoing(true);
                } else {
                    mBuilder.setAutoCancel(true);
                }
                mNotificationManager.notify(this.notifyID, mBuilder.build());
            }
        }
        return 1;
    }
}

1 个答案:

答案 0 :(得分:-1)

与版本> = 8.0一样,更新了秀场通知,您必须按以下方式定义频道

String CHANNEL_ID = "my_channel_01";            // The id of the channel.

然后将通道ID用于通知生成器

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "01")
            .setContentTitle(title)
            .setSmallIcon(R.mipmap.ic_notification)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(message))
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setChannelId(CHANNEL_ID)
            .setContentIntent(pendingIntent);

然后添加下面的版本> = 8.0的代码

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {       // For Oreo and greater than it, we required Notification Channel.
       CharSequence name = "My New Channel";                   // The user-visible name of the channel.
        int importance = NotificationManager.IMPORTANCE_HIGH;

        NotificationChannel channel = new NotificationChannel(CHANNEL_ID,name, importance); //Create Notification Channel
        notificationManager.createNotificationChannel(channel);
    }

    notificationManager.notify(notifyID /* ID of notification */, notificationBuilder.build());

我希望,这将帮助您在状态=> 8.0的状态栏中显示通知