友盟推送通知栏问题

在使用友盟推送的过程中,由于升级了推送sdk,推送的时候log能够打印出推送的消息,但是通知栏不会弹出,这个问题找了好久都没找到原因,代码又不能找到之前的版本。

这是我升级到的最新SDK

    //基础组件库依赖(必须)
    implementation 'com.umeng.umsdk:common:9.1.0'
//    implementation 'com.umeng.umsdk:utdid:1.5.2'

    //友盟push相关依赖(必须)
    implementation 'com.umeng.umsdk:push:6.1.0'

 

log能接收到消息

 

解决方案: 当收到消息的时候,自定义通知栏,自己处理点击事件

1.自定义服务,接收友盟的消息

2.记得在AndroidManifest.xml 加推送服务

<!-- 友盟推送service -->
<service
    android:name=".service.YouMengPushIntentService"
    android:enabled="true"
    android:exported="false"
    android:permission="android.permission.BIND_JOB_SERVICE"
    android:process=":push" />

3.YouMengPushIntentService代码,自己处理了弹出通知栏


public class YouMengPushIntentService extends UmengMessageService {
    NotificationManager manager;
    int id;

    public void getNotification(Context context, String title, String msg, String msgBody) {
        LogUtil.e("YouMengPushIntentService", "getNotification");

        manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        id = (int) (System.currentTimeMillis() / 1000);
        //点击和取消的监听事件
        Intent intentClick = new Intent(this, NotificationClickReceiver.class);
        intentClick.putExtra("msg", msgBody);
        intentClick.setAction(NotificationClickReceiver.DOWN_ACTION); //点击
        PendingIntent pendingIntentClick = PendingIntent.getBroadcast(this, id, intentClick, PendingIntent.FLAG_ONE_SHOT);

        Intent intentCancel = new Intent(this, NotificationClickReceiver.class);
        intentCancel.setAction(NotificationClickReceiver.CLOSE_ACTION);//取消
        PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(this, id, intentCancel, PendingIntent.FLAG_ONE_SHOT);

        //判断8.0,若为8.0型号的手机进行创下一下的通知栏
        if (Build.VERSION.SDK_INT >= 26) {
            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.mipmap.ic_launcher)
                    .setWhen(System.currentTimeMillis())
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                    .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.mipmap.ic_launcher)
                    .setWhen(System.currentTimeMillis())
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                    .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());
            }
        }
    }

    //取消通知
    public void cancelNotify() {
        manager.cancel(id);
    }

    /*{"display_type":"notification","extra":{"type":"1"},
    "body":{"after_open":"go_custom","ticker":"月底提现高峰930","custom":"{“url”:“www”}","title":"月底提现高峰930","play_sound":"true","play_lights":"false","play_vibrate":"false","text":"关系到您的钱 这里一定要看一下!188"},"msg_id":"uabnxim160238004614211"}


        {"display_type":"notification","extra":{"type":"1"},"
        body":{"after_open":"go_app","ticker":"月底提现高峰930","title":"月底提现高峰930","play_sound":"true","play_lights":"false","play_vibrate":"false","text":"关系到您的钱 这里一定要看一下!188"},"msg_id":"uabo0r0160238055233711"}
    */
    @Override
    public void onMessage(Context context, Intent 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取得消息体
            LogUtil.e("YouMengPushIntentService", "onMessage");

            final String message = intent.getStringExtra("body");
            if (TextUtils.isEmpty(message)) {
                return;
            }
            final UMessage msg = new UMessage(new JSONObject(message));
            getNotification(context, msg.title, msg.text, message);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3.NotificationClickReceiver 点击通知栏的监听代码,这里可以处理推送过来的消息,看你要打开首页或是打开其他页面,记住打开页面的时候需要data.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//需为Intent添加FIntent.FLAG_ACTIVITY_NEW_TASK,否则无法启动Activity。


public class NotificationClickReceiver extends BroadcastReceiver {

    public final static String DOWN_ACTION = "DownClick";
    public final static String CLOSE_ACTION = "CloseClick";

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        String message = intent.getStringExtra("msg");
        try {
            final UMessage msg = new UMessage(new JSONObject(message));
            Log.e("NotifyReceiver", "onReceive: action = " + action + "==msg==" + message);
            switch (action) {
                case DOWN_ACTION:
                    if ("go_app".equals(msg.after_open)) { //打开应用
                        Intent data = new Intent(intent);
                        data.setClass(context, MainActivity.class);
                        data.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//需为Intent添加Flag:Intent.FLAG_ACTIVITY_NEW_TASK,否则无法启动Activity。
                        context.startActivity(data);
                    } else if ("go_custom".equals(msg.after_open)) {//自定义行为
                        PublicStartMethod.go_custom(msg.custom, context);
                    }
                    break;
                case CLOSE_ACTION:
                    //处理第2个按钮事件,关闭通知
                    Logger.d("=======", "CLOSE_ACTION");
                    break;
                default:
                    break;
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}

4..记得在AndroidManifest.xml 添加

 <receiver
            android:name="com.taosha.umeng.NotificationClickReceiver"
            android:enabled="true"
            android:exported="true">
        </receiver>

到这里,已经可以弹出通知栏了,也处理了通知栏点击的处理事件 ,结束。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值