【问题标题】:Android : resuming an app after an opening notification pushAndroid:在打开通知推送后恢复应用
【发布时间】:2017-04-20 09:23:57
【问题描述】:

这是我的问题:

我有一个包含 3 个活动的应用程序。

-SplashscreenActivity(启动器 - 主)

-登录活动

-HomeActivity

--应用程序被杀死:-- 正常流程:

  1. 当我通过仪表板图标运行应用程序时,我经历了启动画面(我已登录),然后出现在主页上。 如果我将应用程序置于后台并通过仪表板图标将其启动,我将返回主页。一切正常。

--应用程序被杀死:-- 但是,对于以下过程:

  1. 我收到推送通知。我通过这个推送运行应用程序,我经历了飞溅,然后来到了家。 但是,如果我将应用程序放在后台,并通过仪表板图标将其启动,我会再次系统地通过启动。这就是问题所在!

这是我的 manifest.xml (EDITED)

<activity           
    android:name="my.package.SplashscreenActivity"
    android:label="@string/app_name"
    android:noHistory="false"
    android:screenOrientation="portrait">
    <intent-filter>
         <action android:name="android.intent.action.MAIN"/>
         <category android:name="android.intent.category.LAUNCHER"/>
    </intent-filter>
</activity>

<activity
    android:name="my.package.LoginActivity"
    android:label="@string/app_name"
    android:noHistory="false"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="adjustResize">
</activity>

<activity
    android:name="my.package.HomeActivity"
    android:label="@string/app_name"
    android:noHistory="false"
    android:launchMode="singleTask"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="adjustResize">
</activity>

这是我的推送通知 BroadcastReceiver (sendNotificationMethod)

private void sendNotification(String message, String title, int count, String type, long threadId)
{
    Activity m_activity;
    if (App.getInstance().m_currentActivity != null && App.getInstance().isOnPause())
        m_activity = App.getInstance().m_currentActivity;
    else
        m_activity = null;

    PendingIntent pendingIntent = null;
    Intent intent = null;

    if (m_activity != null)
    {
        intent = new Intent(this, m_activity.getClass());
        intent.putExtra("type", type);
        intent.setData((Uri.parse("foobar://" + SystemClock.elapsedRealtime())));
        pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    }
    else
    {
        PackageManager pm = getPackageManager();
        intent = pm.getLaunchIntentForPackage("my.package");
        intent.setAction(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtra("type", type);
        pendingIntent = PendingIntent.getActivity(App.getInstance(), 0, intent, 0);
    }

    Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.logo_push_lolilol)
            .setColor(App.getInstance().getResources().getColor(R.color.colorPrimary))
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)
            .setStyle(new NotificationCompat.BigTextStyle()
                    .bigText(message));

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


    notificationManager.notify((int)threadId, notificationBuilder.build());
}

你有“解决办法”吗?

提前谢谢你。

【问题讨论】:

  • 删除这个android:launchMode="singleTop"
  • @DheerubhaiBansal 它不会改变任何东西。

标签: java android android-intent push-notification


【解决方案1】:

试试这个

intent.setAction("android.intent.action.MAIN");
intent.addCategory("android.intent.category.LAUNCHER");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);

【讨论】:

  • 问题依然存在......但我认为问题不是第一次启动应用程序的pendingIntent,而是应用程序恢复。 pendingIntent 可以正常启动应用,但是当我把应用放到后台再打开时,出现了问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多