Android Notification Notification Compatible with Android 8.0 and below

Picking up Android again, I really feel the rapid development of technology. Maybe if you don't do it for a year, it's already an earth-shaking change. God knows why this happens. . .

Android notifications are a very frequently used feature. I've been fiddling with this for a day now.

Android 8.0 requires the concept of a channel for the creation and redefinition of Notification. Did not go into detail.

Looking directly at the implementation code, it is compatible with 7.0 and 8.0 --- Note that the 8.0 code fails to run in the 7.0 simulator. The 7.0 code also fails to run on the 8.0 emulator.

Weakly speaking, what about compatibility? Adapted from other blog posts on the Internet.

package com.example.pengda.android_activity.services;

/**
 * Created by pengda on 2018/4/20.
 */
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;


import com.example.pengda.android_activity.R;


public class NotificationUtils extends ContextWrapper {

    private NotificationManager manager;
    public static final String id = "channel_1";
    public static final String name = "channel_name_1";
    public  Notification notification;
    public NotificationUtils(Context context){
        super(context);
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    public void createNotificationChannel(){
        NotificationChannel channel = new NotificationChannel(id, name, NotificationManager.IMPORTANCE_HIGH);
        getManager().createNotificationChannel(channel);
    }
    private NotificationManager getManager(){
        if (manager == null){
            manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        }
        return manager;
    }
    @RequiresApi(api = Build.VERSION_CODES.O)
    public Notification.Builder getChannelNotification(String title, String content){
        return new Notification.Builder(getApplicationContext(), id)
                .setContentTitle(title)
                .setContentText(content)
                .setSmallIcon(android.R.drawable.stat_notify_more)
                .setAutoCancel(true);
    }
    public Notification.Builder getNotification_25(String title, String content){
//        return new NotificationCompat.Builder(getApplicationContext())
//                .setContentTitle(title)
//                .setContentText(content).
//        setSmallIcon( R.mipmap.ic_launcher).setLargeIcon( BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
//                .setAutoCancel(true);
return new Notification.Builder(this).setTicker("123").
              
                setSmallIcon(R.mipmap.ic_launcher).setLargeIcon( BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
                .setContentText("123").setContentTitle( "ashdash" );

    }
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void sendNotification(String title, String content){
        if (Build.VERSION.SDK_INT>=26){
            createNotificationChannel();
            this.notification = getChannelNotification
                    (title, content).build();
            getManager().notify(1,notification);
        }else{
            this.notification = getNotification_25(title, content).build();
            getManager().notify(1,notification);
        }
    }



    public Notification getNotification(){

        return this.notification;
    }
}

I refer to this blog post for this tool class https://blog.csdn.net/z642385985/article/details/78583980?locationNum=9&fps=1 


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324683589&siteId=291194637