gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 06:02:42 25 4
gpt4 key购买 nike

我想通过单击按钮在应用程序图标上设置通知号码。我尝试理解并遵循多种方法,但没有一个有效。

我可以简单地使用 channel 发送通知,但我也想在应用程序图标上设置通知号码。

下面是我的代码:

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

最佳答案

您只能通过每次想要更新通知号码时更改应用的图标来实现。

Try this.

关于java - 如何通过单击按钮在应用程序图标上设置通知号码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54069949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com