如何将应用内的新通知总数发送到图标

问题描述

应用通过 FirebaseCloudMessaging 接收通知通知正常到达。

我想设置一个计数器并将新通知的总数发送到应用程序内的图标。网上有很多关于图标的信息,我可以安装它们。但是如何将所有新通知数量转移到图标上?本来想装一个Listener,但是网上没有找到合适的资料,主要是写所有应用的listener。而你只需要收听我的通知频道。这怎么办?

这就是我收到通知的方式。

OreoAndAboveNotification.java

package com.sg.test1.notifications;

import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.notificationmanager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Contextwrapper;
import android.net.Uri;
import android.os.Build;

public class OreoAndAboveNotification extends Contextwrapper {

    private static final String ID = "some_id";
    private static final String NAME = "test1";

    private notificationmanager notificationmanager;

    public OreoAndAboveNotification(Context base) {
        super(base);
        if (Build.VERSION.SDK_INT>Build.VERSION_CODES.O){
            createChanel();
        }
    }

    @TargetApi(Build.VERSION_CODES.O)
    private void createChanel() {
        NotificationChannel notificationChannel = new NotificationChannel(ID,NAME,notificationmanager.IMPORTANCE_DEFAULT);
        notificationChannel.enableLights(true);
        notificationChannel.enableVibration(true);
        notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
        getManager().createNotificationChannel(notificationChannel);

    }

    public notificationmanager getManager(){
        if (notificationmanager == null){
            notificationmanager = (notificationmanager)getSystemService(Context.NOTIFICATION_SERVICE);

        }
        return notificationmanager;
    }

    @TargetApi(Build.VERSION_CODES.O)
    public Notification.Builder getoNotifications(String title,String body,PendingIntent pendingIntent,Uri soundUri,String icon){
        return new Notification.Builder(getApplicationContext(),ID)
                .setContentIntent(pendingIntent)
                .setContentTitle(title)
                .setContentText(body)
                .setSound(soundUri)
                .setAutoCancel(true)
                .setSmallIcon(Integer.parseInt(icon));

    }
}

FirebaseMessaging.java

package com.sg.test1.notifications;

import android.app.Notification;
import android.app.notificationmanager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.ringtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.core.app.NotificationCompat;

import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.sg.test1.PrChatActivity;

public class FirebaseMessaging extends FirebaseMessagingService {


    @Override
    public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        SharedPreferences sharedPreferences = getSharedPreferences("SP_USER",MODE_PRIVATE);
        String savedCurrentUser = sharedPreferences.getString("Current_USERID","None");

        String sent = remoteMessage.getData().get("sent");
        String user = remoteMessage.getData().get("user");
        FirebaseUser fUser = FirebaseAuth.getInstance().getCurrentUser();
        if (fUser != null && sent.equals(fUser.getUid())) {
            if (!savedCurrentUser.equals(user)) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

                    sendAndNotification(remoteMessage);
                }
                else {
                    sendnormNotification(remoteMessage);
                }
            }
        }
    }

    private void sendnormNotification(RemoteMessage remoteMessage) {
        String user = ""+remoteMessage.getData().get("user");
        String icon = remoteMessage.getData().get("icon");
        String title = remoteMessage.getData().get("title");
        String body = remoteMessage.getData().get("body");

        RemoteMessage.Notification notification = remoteMessage.getNotification();
        int i = Integer.parseInt(user.replaceAll("[\\D]",""));
        Intent intent = new Intent(this,PrChatActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("hisUid",user);
        intent.putExtras(bundle);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,i,intent,PendingIntent.FLAG_ONE_SHOT);

        Uri defSoundUri = ringtoneManager.getDefaultUri(ringtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                .setSmallIcon(Integer.parseInt(icon))
                .setContentText(body)
                .setContentTitle(title)
                .setAutoCancel(true)
                .setSound(defSoundUri)
                .setContentIntent(pendingIntent);

        notificationmanager notificationmanager = (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);

        int j = 0;
        if (i > 0) {
            j = i;
        }
        notificationmanager.notify(j,builder.build());

    }

    private void sendAndNotification(RemoteMessage remoteMessage) {
        String user = remoteMessage.getData().get("user");
        String icon = remoteMessage.getData().get("icon");
        String title = remoteMessage.getData().get("title");
        String body = remoteMessage.getData().get("body");

        RemoteMessage.Notification notification = remoteMessage.getNotification();
        int i = Integer.parseInt(user.replaceAll("[\\D]",PendingIntent.FLAG_ONE_SHOT);

        Uri defSoundUri = ringtoneManager.getDefaultUri(ringtoneManager.TYPE_NOTIFICATION);

        OreoAndAboveNotification notification1 = new OreoAndAboveNotification(this);
        Notification.Builder builder = notification1.getoNotifications(title,body,pendingIntent,defSoundUri,icon);

        int j = 0;
        if (i>0) {
            j = i;
        }
        notification1.getManager().notify(j,builder.build());
    }

    @Override
    public void onNewToken(@NonNull String s) {
        super.onNewToken(s);
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        if (user !=null){
            updatetoken(s);
        }
    }

    private void updatetoken(String tokenRefresh){
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Tokens");
        Token token = new Token(tokenRefresh);
        ref.child(user.getUid()).setValue(token);
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)