php友盟收不到,Android10收不到友盟推送

package com.xxx.yyy.push;

import android.app.Notification;

import android.app.NotificationChannel;

import android.app.NotificationManager;

import android.app.PendingIntent;

import android.app.Service;

import android.content.Context;

import android.content.Intent;

import android.graphics.Color;

import android.os.Build;

import android.os.IBinder;

import android.provider.MediaStore;

import com.xxx.yyy.R;

import com.umeng.message.UTrack;

import com.umeng.message.entity.UMessage;

import org.json.JSONException;

import org.json.JSONObject;

import java.util.Random;

public class MyNotificationService extends Service{

public static UMessage oldMessage = null;

@Override

public void onCreate(){

super.onCreate();

}

@Override

public int onStartCommand(Intent intent, int flags, int startId){

if (intent == null) {

return super.onStartCommand(null, flags, startId);

}

String message = intent.getStringExtra("UmengMsg");

try {

UMessage msg = new UMessage(new JSONObject(message));

if (oldMessage != null) {

UTrack.getInstance(getApplicationContext()).setClearPrevMessage(true);

UTrack.getInstance(getApplicationContext()).trackMsgDismissed(oldMessage);

}

showNotification(msg);

} catch (JSONException e) {

e.printStackTrace();

}

return super.onStartCommand(intent, flags, startId);

}

private void showNotification(UMessage msg){

int id = new Random(System.nanoTime()).nextInt();

oldMessage = msg;

NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

if (manager != null) manager.cancelAll();

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

Notification.Builder mBuilder = new Notification.Builder(this, this.getPackageName())

.setContentTitle(msg.title)

.setContentText(msg.text)

.setTicker(msg.ticker)

.setWhen(System.currentTimeMillis())

.setSmallIcon(R.mipmap.ic_launcher)

.setContentIntent(getClickPendingIntent(this, msg))

.setDeleteIntent(getDismissPendingIntent(this, msg))

.setChannelId(this.getPackageName())

.setAutoCancel(true);

if (manager != null) {

// 用户可以看到的通知渠道的名字

CharSequence name = getResources().getString(R.string.app_name);

// 用户可看到的通知描述

String description = getResources().getString(R.string.app_description);

// 构建NotificationChannel实例

NotificationChannel notificationChannel =

new NotificationChannel(this.getPackageName(), name, NotificationManager.IMPORTANCE_HIGH);

// 配置通知渠道的属性

notificationChannel.setDescription(description);

// 设置通知出现时的闪光灯

notificationChannel.enableLights(true);

notificationChannel.setLightColor(Color.RED);

// 设置通知出现时的震动

notificationChannel.enableVibration(true);

notificationChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 100});

// 在notificationManager中创建通知渠道

manager.createNotificationChannel(notificationChannel);

manager.notify(id, mBuilder.build());

}

} else {

Notification.Builder mBuilder = new Notification

.Builder(this)

.setContentTitle(msg.title)

.setContentText(msg.text)

.setTicker(msg.ticker)

.setWhen(System.currentTimeMillis())

.setSmallIcon(R.mipmap.ic_launcher)

.setContentIntent(getClickPendingIntent(this, msg))

.setDeleteIntent(getDismissPendingIntent(this, msg))

.setWhen(System.currentTimeMillis())

.setDefaults(Notification.DEFAULT_ALL)

.setSound(MediaStore.Audio.Media.INTERNAL_CONTENT_URI)

.setAutoCancel(true);

if (manager != null) manager.notify(id, mBuilder.build());

}

}

public PendingIntent getClickPendingIntent(Context context, UMessage msg){

Intent clickIntent = new Intent();

clickIntent.setClass(context, NotificationBroadcast.class);

clickIntent.putExtra(NotificationBroadcast.EXTRA_KEY_MSG,

msg.getRaw().toString());

clickIntent.putExtra(NotificationBroadcast.EXTRA_KEY_ACTION,

NotificationBroadcast.ACTION_CLICK);

return PendingIntent.getBroadcast(context,

(int) (System.currentTimeMillis()),

clickIntent, PendingIntent.FLAG_CANCEL_CURRENT);

}

public PendingIntent getDismissPendingIntent(Context context, UMessage msg){

Intent deleteIntent = new Intent();

deleteIntent.setClass(context, NotificationBroadcast.class);

deleteIntent.putExtra(NotificationBroadcast.EXTRA_KEY_MSG,

msg.getRaw().toString());

deleteIntent.putExtra(

NotificationBroadcast.EXTRA_KEY_ACTION,

NotificationBroadcast.ACTION_DISMISS);

return PendingIntent.getBroadcast(context,

(int) (System.currentTimeMillis() + 1),

deleteIntent, PendingIntent.FLAG_CANCEL_CURRENT);

}

@Override

public IBinder onBind(Intent intent){

return null;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值