简体   繁体   English

如何通过单击按钮在应用程序图标上设置通知编号?

[英]How to set notification number on app icon by button click?

I want to set notification number on the app icon by button click.我想通过单击按钮在应用程序图标上设置通知编号。 I have tried to understand and follow several methods but none of them worked.我试图理解并遵循几种方法,但没有一种方法有效。

I can send notification using simply using channel but I also want to set notification numbers on app icon.我可以简单地使用频道发送通知,但我也想在应用程序图标上设置通知编号。

Here below my code:下面是我的代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button Gn = (Button) findViewById(R.id.gn);

        Gn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                shownotifications();
            }
        });
    }

    private void shownotifications() {
        //channel
        String id = "main_channel";
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            CharSequence name = "Channel Name";
            String description = "Channel Description";
            int importance = NotificationManager.IMPORTANCE_HIGH;
            NotificationChannel notificationChannel = new NotificationChannel(id, name, importance);
            notificationChannel.setDescription(description);
            notificationChannel.enableLights(true);
            notificationChannel.setLightColor(Color.WHITE);
            notificationChannel.enableVibration(false);

            if (notificationManager != null) {
                notificationManager.createNotificationChannel(notificationChannel);
            }
        }

        //notifications
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, id)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon))
                .setContentTitle("Notification Title")
                .setContentText("This is the description of this notification......")
                .setLights(Color.WHITE, 500, 5000)
                .setColor(Color.RED)
                .setDefaults(Notification.DEFAULT_SOUND);

        Intent nI = new Intent(this, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, nI, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationBuilder.setContentIntent(contentIntent);

        NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
        notificationManagerCompat.notify(1000, notificationBuilder.build());
    }
}

You can only do it by changing your app's icon everytime you want to update the notification number.您只能通过在每次要更新通知编号时更改应用程序的图标来实现。

Try this. 尝试这个。

使用此链接https://github.com/leolin310148/ShortcutBadger设置按钮点击计数,只要你想更新计数用户以前的计数增加

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM