Nativescript-如何在按下通知时将应用程序带到前台 [英] Nativescript - How to bring app to foreground when notification is pressed

查看:26
本文介绍了Nativescript-如何在按下通知时将应用程序带到前台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个原生Android代码,以便在按下通知时打开我的应用程序。如果应用程序已打开(无论是在前台还是在后台运行),我希望单击通知将应用程序带到最前面,而不重新启动它,以便保留其状态。

我尝试了以下代码(仅显示相关代码):



        ///////// Create an activity on tap (intent)
        const Intent = android.content.Intent;
        const PendingIntent = android.app.PendingIntent;
        // Create an explicit intent for an Activity in your app
        const intent = new Intent(context, com.tns.NativeScriptActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);


        const pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

        ///////// Creating a notification 
        var NotificationCompat = android.support.v4.app.NotificationCompat;
        const builder = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(android.R.drawable.btn_star_big_on)
            .setContentTitle(title)
            .setContentText(message)
            .setStyle(
                new NotificationCompat.BigTextStyle()
                .bigText("By default, the notification's text content is truncated to fit one line.")
                )
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            // Set the intent that will fire when the user taps the notification
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);

        ///////// Show the notification
        notificationManager.notify(NOTIFICATION_ID, builder.build());

但这将打开应用程序,而不保留其状态。

按照建议here,我还尝试模拟从启动程序按下应用程序图标,这样应用程序就会被带到前台,而不会重新创建Nativescript活动。

        const packageName = context.getPackageName();
        console.log('Package name: ',packageName);

        const emulateLaunchByAppIconIntent = context.getPackageManager()
            .getLaunchIntentForPackage(packageName)
            .setPackage(null)
            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

        const pendingIntent_emulated = PendingIntent.getActivity(context, 0, emulateLaunchByAppIconIntent, 0);


        ///////// Creating a notification 
        var NotificationCompat = android.support.v4.app.NotificationCompat;
        const builder = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setSmallIcon(android.R.drawable.btn_star_big_on)
            .setContentTitle(title)
            .setContentText(message)
            .setStyle(
                new NotificationCompat.BigTextStyle()
                .bigText("By default, the notification's text content is truncated to fit one line.")
                )
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            // Set the intent that will fire when the user taps the notification
            .setContentIntent(pendingIntent_emulated)
            .setAutoCancel(true);

        ///////// Show the notification
        notificationManager.notify(NOTIFICATION_ID, builder.build());

这确实使应用程序脱颖而出,但未保留其状态(即使应用程序已在前台-它已重新加载应用程序)。

然后,当应用程序刚刚发送到后台时,我尝试(手动)按下Nativescript应用程序图标-我发现它会重新启动应用程序,而不仅仅是将其带到前台。

我的问题是-为什么Nativescript应用程序的行为是这样的? 我如何才能让Android只将应用程序带到前台,而不是重新构建新的本机脚本活动?

推荐答案

上面问题中的代码实际起作用-并且确实会将应用程序带到前台,而无需重新启动应用程序,并保留其状态。

即使通过(手动)单击Nativescript应用程序图标也可以重新启动应用程序的原因与开发环境有关。

  1. 当我连接到运行tns run android --bundle

    的Mac时,在物理设备上运行应用程序时发生这种情况
  2. 在模拟器中运行应用程序(通过运行tns run android --bundle或直接从应用程序图标启动应用程序)

    /li>

在未连接到本机脚本开发环境的物理设备上运行应用程序-显示了实际行为-单击通知后应用程序被带到前台,而不重新启动。

有关代码示例的详细信息:

我发现不需要通过使用此代码模拟图标按压来启动应用程序

const emulateLaunchByAppIconIntent = context.getPackageManager()
   .getLaunchIntentForPackage(packageName)
   .setPackage(null)
   .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

const pendingIntent_emulated = PendingIntent.getActivity(context, 0, emulateLaunchByAppIconIntent, 0);

作为以下代码中的PendingIntent.FLAG_UPDATE_CURRENTIntent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED,足以将APP带到前台,而不需要在点击通知后重新启动:

const openActivityIntent = new Intent(context, com.tns.NativeScriptActivity.class);
openActivityIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED | Intent.FLAG_ACTIVITY_NEW_TASK);
const openActivityPendingIntent = PendingIntent.getActivity(context, 0, openActivityIntent, 0);

///////// Creating a notification
const notificationManager = <NotificationManager> context.getSystemService(NotificationManager.class);
var NotificationCompat = android.support.v4.app.NotificationCompat;

const builder = new NotificationCompat.Builder(context, CHANNEL_ID)
    .setSound(soundUri)
    .setSmallIcon(android.R.drawable.ic_lock_idle_alarm)
    .setContentTitle(title)
    .setContentText(message)
    .setStyle(
        new NotificationCompat.BigTextStyle()
        .bigText('More explaination text if needed. Disabled for now.')
        )
    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
    // Set the intent that will fire when the user taps the notification
    .setContentIntent(openActivityPendingIntent)
    .setWhen(scheduledTime)
    .setAutoCancel(true)
    .build();


///////// Show the notification
notificationManager.notify(NOTIFICATION_ID, builder);

希望这能在您遇到相同问题时对您有所帮助。

这篇关于Nativescript-如何在按下通知时将应用程序带到前台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆