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

我正在开发一个需要关闭的嵌入式应用程序之一,如果我通过代码隐藏了应用程序的图标,那么当我单击系统灰色通知启动它时,不会调用applicaton。

>

我已经尝试在示例应用程序中对此进行模拟,但它甚至在这里也无法正常工作。没有启动器图标的Google for Apps是否有限制,不能从通知中调用?

我的Applicaiton类代码:

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,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>

xiaoxiaov0 回答:单击没有图标的应用程序中的通知不会调用该应用程序

private NotificationManager alarmNotificationManager;
private void showNotification(Context context,String msg) {
    PendingIntent contentIntent = PendingIntent.getActivity(context,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());

    }
}

尝试使用此方法显示通知。

,

只是找到原因并想与大家分享。

为了完全隐藏应用程序图标,我禁用了启动器活动,同时这样做的意图过滤器也被禁用了,因为整个组件都被禁用了。因为单击通知时正在启动的意图会将您带到仅基于意图过滤器的启动器活动,所以会抛出该意图,但是启动器活动被禁用,其意图过滤器也被禁用,导致该意图不起作用。

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

本文链接:https://www.f2er.com/3167442.html

大家都在问