【问题标题】:How to show a Notification on Oreo?如何在奥利奥上显示通知?
【发布时间】:2018-03-31 17:25:46
【问题描述】:

通知没有显示在上面的 V26 API.Notification cshown on v25 但是当我在 v27 上检查它时,通知没有出现。我的代码 启动服务并创建通知。

内部维护 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 扩展服务

onCreateMethod 我调用方法 shownotification 将调用广播接收器。

 @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 类

在这个课程中,我构建了通知并通知要显示的通知。

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

内部 buildNotification() 方法

     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());
        }

但在 v26 上没有出现上述通知。 请帮帮我。

【问题讨论】:

  • 想必buildNotification()有问题,比如没有设置通知通道,使用不了。您的问题没有显示buildNotification() 的实现,因此我们无法对此发表评论。
  • @CommonsWare 我已经粘贴了 buildnotificatio 方法,请看。谢谢

标签: android notifications android-notifications android-notification-bar


【解决方案1】:

创建一个新类并将其命名为 NotificationUtils,扩展 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;
    }
}

我们在 Notification.Builder (Android API 25) 构造函数中指定应该将哪个通知发送到通道,我们将通道 ID 作为第二个参数传递给它。

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);
}

让我们从任何要测试的活动创建推送通知,

放入全局范围:

private NotificationUtils mNotificationUtils;

在onCreate中初始化

mNotificationUtils = new NotificationUtils(this);

在按钮点击测试时生成通知:

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());
                }
            }
        });

【讨论】:

  • 此代码是否适用于比果冻豆更大的每个版本?或者它仅适用于 V26+。对此有点困惑。感谢您的解决方案
猜你喜欢
  • 2017-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多