How to show a Notification on Oreo?

蓝咒 提交于 2020-01-11 14:37:12

问题


Notification doesn't shown on above V26 API.Notification cshown on v25 but when i checked it on v27 than the notification doesn't appear. My code to StartService and create notification.

Inside Mainctivity onCreate()

  Intent intent1 = new Intent(this, NotificationService.class);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                ContextCompat.startForegroundService(this, intent1);
            } else {
                startService(intent1);
            }

NotificationService extends Service

onCreateMethod i call the method shownotification which will invoke the broadcastreceiver.

 @Override
public void onCreate() {
    super.onCreate();
    session = SessionManager.getInstance(getApplicationContext());
    showNotification();
}

private void showNotification() {
    if (session.isNotificationOn() && session.isLogged()) {
        Intent notificationIntent = new Intent(this, NotificationReceiver.class);
        notificationIntent.putExtra(NotificationReceiver.CODE, NotificationReceiver.TYPE_START_CODE);
        sendBroadcast(notificationIntent);
    }
}

NotificationReceiver class

At this class, i build the notification and notify the notification to show.

 @Override
    public void onReceive(Context context, Intent intent) {
        buildNotification(context);
    }

Inside buildNotification() method

     private void buildNotification(Context context) {
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context,NOTIFICATION_CHANNEL_ID)
                .setSmallIcon(R.drawable.notification)
                .setAutoCancel(false)
                .setOngoing(true)
                .setPriority(NotificationCompat.PRIORITY_MAX)
                .setContentTitle(context.getString(R.string.app_name))  .setContentText(context.getString(R.string.notification_pull_down_information))
                .setShowWhen(false)
                .setColor(Color.BLACK);
        String CHANNEL_ID = "my_channel_01";// The id of the channel.
        CharSequence name = context.getString(R.string.app_name);// The user-visible name of the channel.
        int importance = NotificationManager.IMPORTANCE_HIGH;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        }
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            manager.createNotificationChannel(mChannel);
        }
        manager.notify(notificatioCode, builder.build());
        }

But on v26 above notification doesn't appear. Please help me.


回答1:


Create a new class and name it NotificationUtils, extending ContextWrapper.

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.graphics.Color;

public class NotificationUtils extends ContextWrapper {

    private NotificationManager mManager;
    public static final String ANDROID_CHANNEL_ID = "com.testandroid.pushnotifications.ANDROID";

    public static final String ANDROID_CHANNEL_NAME = "ANDROID CHANNEL";

    public NotificationUtils(Context base) {
        super(base);
        createChannels();
    }

    public void createChannels() {

        // create android channel
        NotificationChannel androidChannel = new NotificationChannel(ANDROID_CHANNEL_ID,
                ANDROID_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
        // Sets whether notifications posted to this channel should display notification lights
        androidChannel.enableLights(true);
        // Sets whether notification posted to this channel should vibrate.
        androidChannel.enableVibration(true);
        // Sets the notification light color for notifications posted to this channel
        androidChannel.setLightColor(Color.GREEN);
        // Sets whether notifications posted to this channel appear on the lockscreen or not
        androidChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

        getManager().createNotificationChannel(androidChannel);



    private NotificationManager getManager() {
        if (mManager == null) {
            mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        }
        return mManager;
    }
}

We specify which notification should be sent to a channel in the Notification.Builder (Android API 25) constructor, where we pass it the channel id as the second argument.

public Notification.Builder getAndroidChannelNotification(String title, String body) {
    return new Notification.Builder(getApplicationContext(), ANDROID_CHANNEL_ID)
            .setContentTitle(title)
            .setContentText(body)
            .setSmallIcon(android.R.drawable.stat_notify_more)
            .setAutoCancel(true);
}

Let's create push notification from any of the activity to test,

Put in global scope:

private NotificationUtils mNotificationUtils;

Initialize in onCreate

mNotificationUtils = new NotificationUtils(this);

Generate notification on button click to test:

buttonSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String title = "Test Notification";
                String text= "Oreo Device Push Notification is here";

                if(!TextUtils.isEmpty(title) && !TextUtils.isEmpty(text)) {
                    Notification.Builder nb = mNotificationUtils.
                            getAndroidChannelNotification(title, text);

                    mNotificationUtils.getManager().notify(101, nb.build());
                }
            }
        });


来源:https://stackoverflow.com/questions/49590489/how-to-show-a-notification-on-oreo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!