友盟推送注册成功但是收不到推送

友盟推送注册成功之后。但是收不到推送

注意:(还要写service接收推送。然后还要定义通知栏。)官方文档上都没有真的垃圾。友盟推送注册成功但是收不到推送_第1张图片

 


 

public class YouMengPushIntentService extends UmengMessageService {

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

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

    @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);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

你可能感兴趣的:(android,友盟推送注册成功但是收不到推送,友盟推送)