友盟推送8.0信息不展示(推送状态却为已送达)

博客指出Android版本过高时原通知栏无法使用,需添加设置。介绍了创建接收消息的service,在清单文件中注册服务,开启友盟接收信息的新进程,可在自定义service中实现通知栏设置、信息拦截等需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

其实这种状况是由于android的版本过高,一开始的通知栏不能使用,需要在原本的通知栏上加一些设置,代码我已经放入service中

 

在appliaction 中的onCreat 方法中进行初始化友盟并获取到 tounk值

PushAgent mPushAgent = PushAgent.getInstance(this);//注册友盟推送
        mPushAgent.enable();
        onUmengPush(mPushAgent);
        device_token = UmengRegistrar.getRegistrationId(this);//友盟所需设备id



/**
     * 设置友盟
     *
     * @param mPushAgent
     */
    private void onUmengPush(PushAgent mPushAgent) {
        mPushAgent.setDebugMode(false);//PushSDK的调试日志默认是输出的.建议调用mPushAgent.setDebugMode(false)关闭日志输出。
        //mPushAgent.setNoDisturbMode(23, 0, 7, 0);//SDK默认在“23:00”到“7:00”之间收到通知消息时不响铃,不振动,不闪灯
        //mPushAgent.setNotificationPlaySound(MsgConstant.NOTIFICATION_PLAY_SERVER); //声音
        //mPushAgent.setNotificationPlayLights(MsgConstant.NOTIFICATION_PLAY_SERVER);//呼吸灯
        //mPushAgent.setNotificationPlayVibrate(MsgConstant.NOTIFICATION_PLAY_SERVER);//振动
        mPushAgent.setDisplayNotificationNumber(2);//通知栏按数量显示
        mPushAgent.setPushIntentServiceClass(YouMengPushIntentService.class); //注册好自己所写的服务,准备接受友盟推送过来的信息
    }

 

 

创建自己接受消息的service,在这里了我有有人感觉很奇怪,友盟的消息为什么使用一个service来获取数据,一开始我也很疑惑,那么我们继续向下看 

public class YouMengPushIntentService extends UmengBaseIntentService {
    @Override
    protected void onMessage(Context context, Intent intent) {
        super.onMessage(context, intent);
        try {
            Intent data = new Intent(intent);
//            data.setClass(context, DialogActivity.class);
//            data.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//需为Intent添加Flag:Intent.FLAG_ACTIVITY_NEW_TASK,否则无法启动Activity。
//            context.startActivity(data);
            //可以通过MESSAGE_BODY取得消息体
            final String message = intent.getStringExtra(BaseConstants.MESSAGE_BODY);
            if (TextUtils.isEmpty(message)) {
                return;
            }
            final UMessage msg = new UMessage(new JSONObject(message));
            getNotification(context, msg.title, msg.text);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void getNotification(Context context, String title, String msg) {
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        int id = (int) (System.currentTimeMillis() / 1000);
        Intent intentClick = new Intent(this, NotificationClickReceiver.class);
        intentClick.putExtra("title", title);
        intentClick.putExtra("msg", msg);
        intentClick.setAction("notification_clicked");
        intentClick.putExtra(NotificationClickReceiver.TYPE, 0); //0代表点击
        PendingIntent pendingIntentClick = PendingIntent.getBroadcast(this, id, intentClick, PendingIntent.FLAG_ONE_SHOT);

        Intent intentCancel = new Intent(this, NotificationClickReceiver.class);
        intentCancel.setAction("notification_cancelled");
        intentCancel.putExtra(NotificationClickReceiver.TYPE, 1); //1代表清除的监听
        PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this, id, intentCancel, PendingIntent.FLAG_ONE_SHOT);

        if (Build.VERSION.SDK_INT >= 26) {  //判断8.0,若为8.0型号的手机进行创下一下的通知栏
            NotificationChannel channel = new NotificationChannel("channel_id", "channel_name", NotificationManager.IMPORTANCE_HIGH);
            if (manager != null) {
                manager.createNotificationChannel(channel);
            }
            Notification.Builder builder = new Notification.Builder(context, "channel_id");
            builder.setSmallIcon(R.drawable.icon_180)
                    .setWhen(System.currentTimeMillis())
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_180))
                    .setContentTitle(title)
                    .setContentText(msg)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntentClick)
                    .setDeleteIntent(pendingIntentCancel);
            manager.notify(id, builder.build());
        } else {
            Notification.Builder builder = new Notification.Builder(context);
            builder.setSmallIcon(R.drawable.icon_180)
                    .setWhen(System.currentTimeMillis())
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.icon_180))
                    .setContentTitle(title)
                    .setContentText(msg)
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntentClick)
                    .setDeleteIntent(pendingIntentCancel);;
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                manager.notify(id, builder.build());
            }
        }
    }
}

 在清单文件中注册服务这是后我们看到最后面的一行代码是开启一个新的进程,这个进程是友盟接受信息的进程,这次解开了我之前的疑惑,这样就可以在自己的service中自定义自己的通知栏,信息拦截等等需求

        <!-- 友盟推送service -->
        <service
            android:name="service.YouMengPushIntentService"
            android:process=":push" />

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值