Android关于极光推送收到空白通知的问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37794706/article/details/78550654

产生此问题的原因:

由于项目里面使用ShortcuBadger第三方库来实现桌面角标,这个桌面角标会发一条空的通知,所以才会出现推送的时候出现空白通知。

compile "me.leolin:ShortcutBadger:1.1.13@aar"//实现桌面角标的第三方库,

如果 项目中不得不使用这两个结合,就再项目中添加一个BadgeIntentService这个服务,这个是demo中有的,在这个里面删除收到的空白通知,这样就可以解决问题了。

BadgeIntentService

package com.clps.plane.service;

import android.app.IntentService;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;

import com.clps.plane.tools.picker.utils.LogUtils;

public class BadgeIntentService extends IntentService {

    private int notificationId = 0;

    public BadgeIntentService() {
        super("BadgeIntentService");
    }

    private NotificationManager mNotificationManager;

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        LogUtils.i("onHandleIntent########");
        if (intent != null) {
            LogUtils.i(intent.hasExtra("badgeCount")+"########" + intent.getAction());
            int badgeCount = intent.getIntExtra("badgeCount", -1);
            mNotificationManager.cancel(notificationId);
            notificationId++;
//            Notification.Builder builder = new Notification.Builder(getApplicationContext())
//                .setContentTitle("")
//                .setContentText("")
//                .setSmallIcon(R.mipmap.ic_logo);
//            Notification notification = builder.build();
////            ShortcutBadger.applyNotification(getApplicationContext(), notification, badgeCount);
//            mNotificationManager.notify(notificationId, notification);
        }
    }
}

添加角标

1.在onRecive里面启动服务

public void onReceive(Context context, Intent intent) {
    try {
        Bundle bundle = intent.getExtras();
        Logger.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: " + printBundle(bundle));
        context.startService(new Intent(context, BadgeIntentService.class));//启动服务


2.在收到通知的时候添加桌面角标

else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
    //显示桌面角标,每收到一条通知就加一个
    try {
        msgNum = msgNum + 1;
        ShortcutBadger.applyCount(context, msgNum);
    } catch (Exception e) {
        e.printStackTrace();
    }


删除角标

我这里删除角标是在MainActivity的onResume方法里面实现的

try {
    MyReceiver.msgNum = 0;//吧消息条数初始化为0
    ShortcutBadger.removeCount(instance); //每次打开app,就清除所有的角标,
} catch (Exception e) {
    e.printStackTrace();
}
new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        instance.startService(new Intent(instance, BadgeIntentService.class).putExtra("badgeCount", 0));
    }
}, 500);






能够快速的解决此问题,还得感谢,极光推送平台的技术支持。

我这里把帮我解决这个问题的技术邮箱分享出来,有问题可以给这个邮箱发邮件,邮箱:support<[email protected]>




觉得有用的,就给妹子一个赞吧,么么哒害羞









猜你喜欢

转载自blog.csdn.net/m0_37794706/article/details/78550654