gpt4 book ai didi

android - 通知在模拟器上有效,但在真实设备上无效

转载 作者:搜寻专家 更新时间:2023-11-01 09:24:34 24 4
gpt4 key购买 nike

我正在尝试在 Samsung Galaxy S7(Android 8.0)上测试我的应用程序,但没有出现通知,我已经检查了应用程序权限和电池设置,它可能是什么?

public void sendNotification(View view)
{

@SuppressWarnings("deprecation")
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

//creating the intent that’ll fire when the user taps the notification
Intent intent = new Intent(getApplicationContext(), ViewActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
mBuilder.setContentIntent(pendingIntent);
//making notification disappear after being clicked
mBuilder.setAutoCancel(true);

mBuilder.setSmallIcon(R.drawable.notification_icon);
mBuilder.setContentTitle("TI connect");
mBuilder.setContentText("New report has been added! Total reports: " + repNo);
mBuilder.setPriority(Notification.PRIORITY_MAX);

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

mNotificationManager.notify(001, mBuilder.build());
}

最佳答案

在 Android 8.0 中需要 Channal

你需要添加这个

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.app_name);
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
assert notificationManager != null;
notificationManager.createNotificationChannel(mChannel);
}

String CHANNEL_ID = "my_channel_01";
notificationBuilder.setChannelId(CHANNEL_ID);

关于android - 通知在模拟器上有效,但在真实设备上无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51667324/

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