Android 单击没有图标的应用程序发出的通知不会调用该应用程序

Android 单击没有图标的应用程序发出的通知不会调用该应用程序,android,android-notifications,android-notification-bar,Android,Android Notifications,Android Notification Bar,我正在开发一个需要安装的嵌入式应用程序,如果我通过代码隐藏了应用程序的图标,那么当我点击系统树通知启动它时,应用程序不会被调用 我曾尝试在一个示例应用程序中模拟这一点,但它在那里甚至不起作用。对于没有启动程序图标的应用程序,谷歌是否有任何限制不能从通知中调用 我的应用程序类别代码: class MainApplication : Application() { override fun onCreate() { super.onCreate() hideAppIcon()

我正在开发一个需要安装的嵌入式应用程序,如果我通过代码隐藏了应用程序的图标,那么当我点击系统树通知启动它时,应用程序不会被调用

我曾尝试在一个示例应用程序中模拟这一点,但它在那里甚至不起作用。对于没有启动程序图标的应用程序,谷歌是否有任何限制不能从通知中调用

我的应用程序类别代码:

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

我的舱单:

<?xml version="1.0" encoding="utf-8"?>



尝试此方法以显示通知

刚刚找到原因,想与大家分享

为了完全隐藏applicationon图标,我禁用了启动程序活动,同时由于整个组件被禁用,因此该活动的意图过滤器也被禁用。由于在单击通知时启动的意图会将您带到仅基于意图筛选器的启动器活动,因此会抛出该意图,但启动器活动被禁用,其意图筛选器也被禁用,从而导致该意图不执行任何操作


希望这对所有想要完全隐藏应用程序图标的人有所帮助。

向我们展示您迄今为止的尝试。我已更新了我的问题详细信息。一旦应用程序启动,它首先隐藏图标,然后显示通知。之后,我杀死了应用程序,但仍然可以单击system trey中的通知打开它。当我这样做时,我不会被带到应用程序主活动。如果我启用了应用程序的用户图标,那么我将进入主活动。尝试了答案,但对最终结果没有影响,因为它仍然没有启动主活动。这似乎不是我们在通知中如何设置意图的问题,而是正在解决意图过滤器的问题。仅供参考,我正在API 26设备上测试它。请尝试阅读本页之前已经阅读过此内容,但找不到与此问题相关的内容。文档中是否有我遗漏的特定内容?我认为您应该将代码片段从应用程序类移动到您的活动中
<?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());

    }
}