android - 单击没有图标的应用程序的通知不会调用该应用程序
问题描述
我正在开发需要关闭的嵌入式应用程序之一,如果我通过代码隐藏应用程序的图标,那么当我单击系统 trey 通知来启动它时,应用程序不会被调用。
我试图在示例应用程序中对此进行模拟,但它甚至在那里也不起作用。没有启动器图标的谷歌应用程序是否有任何限制,不能从通知中调用?
我的应用程序类代码:
class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
hideAppIcon()
showNotification()
}
private fun hideAppIcon() {
val p = packageManager
val componentName = ComponentName(
this,
com.icon.notification.rel.MainActivity::class.java
) // activity which is first time open in manifiest file which is declare as <category android:name="android.intent.category.LAUNCHER" />
p.setComponentEnabledSetting(
componentName,
PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
PackageManager.DONT_KILL_APP
)
}
private fun showNotification(){
// Create the NotificationChannel, but only on API 26+ because
// the NotificationChannel class is new and not in the support library
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val name = getString(R.string.channel_name)
val descriptionText = getString(R.string.channel_description)
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel("local_notification", name, importance).apply {
description = descriptionText
}
// Register the channel with the system
val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
// Create an explicit intent for an Activity in your app
val intent = Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
val builder = NotificationCompat.Builder(this, "local_notification")
.setSmallIcon(android.R.drawable.ic_dialog_email)//@android:drawable/ic_dialog_email
.setContentTitle("My notification")
.setContentText("Hello World!")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true)
with(NotificationManagerCompat.from(this)) {
// notificationId is a unique int for each notification that you must define
notify(1000000001, builder.build())
}
}
}
}
我的活动代码:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
fab.setOnClickListener { view ->
this.finish()
}
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
}
}
}
我的清单:
<?xml version="1.0" encoding="utf-8"?>
<application
android:name=".MainApplication"
android:allowBackup="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
解决方案
private NotificationManager alarmNotificationManager;
private void showNotification(Context context, String msg) {
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, SplashActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int notifyID = 1;
String CHANNEL_ID = "my_channel_01";// The id of the channel.
CharSequence name = "Football90 Reminder";// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
// Create a notification and set the notification channel.
Notification notification = new Notification.Builder(context)
.setContentTitle("Football90")
.setContentText(msg)
.setSmallIcon(R.mipmap.ic_launcher)
.setChannelId(CHANNEL_ID)
.setContentIntent(contentIntent)
.build();
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
mNotificationManager.createNotificationChannel(mChannel);
}
// Issue the notification.
mNotificationManager.notify(notifyID , notification);
}else {
alarmNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
//Create notification
NotificationCompat.Builder alamNotificationBuilder = new NotificationCompat.Builder(
context).setContentTitle("Football90").setSmallIcon(R.mipmap.ic_launcher)
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg).setAutoCancel(true);
alamNotificationBuilder.setContentIntent(contentIntent);
//notiy notification manager about new notification
alarmNotificationManager.notify(1, alamNotificationBuilder.build());
}
}
试试这个方法来显示通知。
推荐阅读
- regex - 正则表达式:用否定解决正则表达式
- xquery - XML Xquery:在主元素中列出多个子元素,而不是多次列出具有不同子元素的相同主元素
- python - 当我使用 Python folium 库将鼠标悬停在世界地图上时,如何显示国家名称和人口?
- java - 在 Java 中合并二叉树
- javascript - Webpack 站点未在 Netlify 上显示我动态添加的图像,但在开发服务器上显示
- ios - 现有已发布应用的 iOS 13 SDK 截止日期混淆
- php - 如何从 Twig 调用控制器的功能
- mysql - 数字和字符串之间的SQL比较
- android - Retrofit 在哪里从服务器中提取数据?
- javascript - 如何判断暴力排序算法何时排序