开发者社区> 问答> 正文

应用研发平台EMAS上用这个代码android8.0以上能收到推送,但在通知栏不显示,请问该如何解决

应用研发平台EMAS上用这个代码android8.0以上能收到推送,但在通知栏不显示,请问该如何解决?

import static android.content.ContentValues.TAG;

import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;

import com.alibaba.sdk.android.push.CloudPushService;
import com.alibaba.sdk.android.push.CommonCallback;
import com.alibaba.sdk.android.push.noonesdk.PushServiceFactory;
import com.alibaba.sdk.android.push.notification.BasicCustomPushNotification;
import com.alibaba.sdk.android.push.notification.CustomNotificationBuilder;
import com.alibaba.sdk.android.push.register.VivoRegister;

public class MyApplication extends Application {
@Override
public void onCreate() {

    super.onCreate();
    initCloudChannel(this);
    createNotificationChannel(this);
}

// 初始化阿里云推送通知服务
private void initCloudChannel(Context applicationContext) {
    PushServiceFactory.init(applicationContext);
    CloudPushService pushService = PushServiceFactory.getCloudPushService();
    pushService.setLogLevel(CloudPushService.LOG_DEBUG);   //仅适用于Debug包,正式包不需要此行

    // 注册推送通知服务
    pushService.register(applicationContext, new CommonCallback() {
        @Override
        public void onSuccess(String response) {
            Log.d(TAG, "推送通知初始化成功");
        }

        @Override
        public void onFailed(String errorCode, String errorMessage) {
            Log.d(TAG, "推送通知初始化失败 -- errorcode:" + errorCode + " -- errorMessage:" + errorMessage);
        }
    });

    // 设置自定义通知样式(可选)
    BasicCustomPushNotification notification = new BasicCustomPushNotification();
    notification.setBuildWhenAppInForeground(false);
    CustomNotificationBuilder.getInstance().setCustomNotification(1, notification);

    // 设置 MessageIntentService
    pushService.setPushIntentService(MessageIntentService.class);

    // 厂商辅助通道初始化
    VivoRegister.register(applicationContext);
}


// 创建一个推送通知通道
private void createNotificationChannel(Application app) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationManager mNotificationManager = (NotificationManager) app.getSystemService(Context.NOTIFICATION_SERVICE);
        // 通知通道的id
        String id = "1";
        // 用户可以看到的通知通道的名字.
        CharSequence name = "推送通知通道";
        // 用户可以看到的通知通道的描述
        String description = "通道描述";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel mChannel = new NotificationChannel(id, name, importance);
        // 配置通知渠道的属性
        mChannel.setDescription(description);
        // 设置通知出现时的闪灯(如果 android 设备支持的话)
        mChannel.enableLights(true);
        mChannel.setLightColor(Color.RED);
        // 设置通知出现时的震动(如果 android 设备支持的话)
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
        //最后在notificationmanager中创建该通知渠道
        mNotificationManager.createNotificationChannel(mChannel);
    }
}

}

展开
收起
2401。 2023-07-16 14:04:50 46 0
3 条回答
写回答
取消 提交回答
  • 可参考这个文档。安卓8.0通知通道设置
    https://help.aliyun.com/document_detail/613896.html
    此回答整理自钉群“应用研发平台EMAS开发者交流群”。

    2023-07-18 13:32:14
    赞同 展开评论 打赏
  • 如果在使用应用研发平台EMAS时,在Android 8.0及以上版本上能够收到推送通知,但通知栏中没有显示通知的话,可能是以下原因导致的:

    1. 通知渠道配置: Android 8.0引入了通知渠道(Notification Channels)的概念,它允许你对不同类型的通知进行分类和管理。确保你在代码中正确地配置了通知渠道,并为推送通知分配了有效的通知渠道ID。同时,检查通知渠道的重要性级别(importance level),确保它设置为足够高的级别以在通知栏中显示通知。

    2. 权限配置: 确保你的应用已经请求并获得了必要的通知权限。在Android 8.0及以上版本中,应用需要动态请求通知权限(Notification Permission),以确保可以在通知栏中显示推送通知。检查你的应用是否已经正确地请求了通知权限,并且用户已经允许了该权限。

    3. 通知栏设置: 某些设备或系统可能具有自定义的通知栏设置,可能会导致某些通知被隐藏或禁止显示。请确保在设备的通知设置中,你的应用的通知权限和通知显示都被启用。

    4. 推送服务商设置: 如果你使用了第三方的推送服务商(如华为、小米等),确保你已经按照他们的指南正确地配置了推送通知相关的设置。不同的推送服务商可能有一些特定的要求或限制。

    2023-07-16 22:51:27
    赞同 展开评论 打赏
  • 北京阿里云ACE会长

    您使用EMAS实现了推送功能,并且在 Android 8.0 及以上版本中能够收到推送通知,但是无法在通知栏中显示。可能是因为Android 8.0及以上版本引入了通知渠道(Notification Channels)机制,需要在代码中创建并指定通知渠道,否则推送通知无法在通知栏中显示。

    为了解决该问题,您可以在代码中创建并设置通知渠道。具体来说,您可以按照以下步骤进行操作:

    创建通知渠道:您可以在应用程序启动时,在Application类的onCreate方法中创建通知渠道。例如:
    ebnf
    Copy
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT);
    channel.setDescription("channel_description");
    notificationManager.createNotificationChannel(channel);
    }
    其中,第一个参数为通知渠道的ID,第二个参数为通知渠道的名称,第三个参数为通知渠道的重要性等级,第四个参数为通知渠道的描述信息。

    设置通知渠道:您可以在接收到推送通知时,设置通知渠道ID,以确保推送通知可以在通知栏中显示。例如:
    reasonml
    Copy
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id")
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("Notification Title")
    .setContentText("Notification Content")
    .setPriority(NotificationCompat.PRIORITY_DEFAULT);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
    notificationManager.notify(notificationId, builder.build());
    其中,第二个参数为通知渠道的ID,第三个参数为通知标题,第四个参数为通知内容,其他参数为通知显示的相关设置。

    2023-07-16 14:21:07
    赞同 展开评论 打赏
来源圈子
更多
收录在圈子:
基于阿里巴巴以及合作伙伴的最佳实践,围绕大前端、云原生领域的相关技术热点(小程序、Serverless、应用中间件、低代码、DevOps)展开行业探讨,与开发者一起探寻云原生时代应用研发的新范式。
相关文档: 移动研发平台
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载