简体   繁体   English

没有调用Android OnNewIntent

[英]Android OnNewIntent not called

I saw several approaches and I tried everything but couldnt make it work.I dont know why it is so complicated, in the docs it looks so easy! 我看到了几种方法,我尝试了一切,但无法使它工作。我不知道为什么它如此复杂,在文档中它看起来如此简单! I want to trigger the OnNewIntent with a notification (the user clicks on it in the notification bar). 我想通过通知触发OnNewIntent(用户在通知栏中单击它)。

Currently I have set my Activity as singleTop 目前我已将Activity设置为singleTop

<activity
    android:name="-.-.-.MainMenuActivity"
    android:launchMode="singleTop"
    android:screenOrientation="portrait" >
</activity>

This is the code where I create the Notification: 这是我创建通知的代码:

public void showNotification(String text, String componentId){
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.logo)
            .setContentTitle("Title")   
            .setAutoCancel(true)
            .setContentText(text);

    // Creates an explicit intent for an Activity in your app
    Intent resultIntent = new Intent(this, MainMenuActivity.class);
    if(!componentId.equalsIgnoreCase("")){                         
        resultIntent.putExtra("componentId", componentId);
    }   
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
    mBuilder.setFullScreenIntent(resultPendingIntent, false);
    mBuilder.setContentIntent(resultPendingIntent);
    NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // mId allows you to update the notification later on.
    mNotificationManager.notify(0, mBuilder.build());
}

This is the OnNewIntent method in my MainMenuActivity: 这是我的MainMenuActivity中的OnNewIntent方法:

@Override
protected void onNewIntent(Intent intent) {
    // TODO Auto-generated method stub
    super.onNewIntent(intent);
    setIntent(intent);

    ...
}

I never get the OnNewIntent call. 我从未接到过OnNewIntent电话。 I dont know what I am doing wrong. 我不知道我做错了什么。 I use only 2 activites in the whole app and the MainMenuActivity comes after the LoginActivity so the MainMenuActivity should always be on top of the stack anyways (I have more fragments where I replace them inside the MainMenuActivity). 我在整个应用程序中只使用了2个活动,并且MainMenuActivity位于LoginActivity之后,因此MainMenuActivity应始终位于堆栈顶部(我有更多的片段,我在MainMenuActivity中替换它们)。

Any help would be appreciated! 任何帮助,将不胜感激! Thank you guys. 感谢你们。

Ok got it working soon after posting my question. 好的,在发布我的问题之后很快就开始了。 I think the key difference in our code is that I pass the flag "PendingIntent.FLAG_UPDATE_CURRENT" to the creation/retrieval of the PendingIntent object. 我认为代码中的关键区别在于我将标志“PendingIntent.FLAG_UPDATE_CURRENT”传递给PendingIntent对象的创建/检索。 This post helped me figure that out. 这篇文章帮我弄明白了。

Notification.Builder mBuilder =
                new Notification.Builder(context)
                .setSmallIcon(R.drawable.notifyicon)
                .setContentTitle(title)
                .setContentText(extras.getString("message"))
                .setAutoCancel(true)
                .setOnlyAlertOnce(false)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setVibrate(new long[] {0,500,250,500});

        if(badgeCount > 1)
            mBuilder.setNumber(badgeCount);
        // Creates an explicit intent for an Activity in your app
        Intent resultIntent = new Intent(context, SiteViewActivity.class);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        resultIntent.putExtra(NOTIFY_INTENT_TYPE_KEY, alertType);

        PendingIntent resultPendingIntent = PendingIntent.getActivity(context, alertType, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mBuilder.setContentIntent(resultPendingIntent);
        NotificationManager notifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notifyMgr.notify(alertType, mBuilder.build());

As first, you should add android:launchMode="singleTop" to your activity definition in manifest file like below 首先,您应该将android:launchMode="singleTop"到清单文件中的活动定义,如下所示

<activity
    android:name="MainActivity"
    android:launchMode="singleTop"
</activity>

Then like Hasan Masud said that, you should add Intent.ACTION_MAIN and Intent.CATEGORY_LAUNCHER to your action like below 然后像Hasan Masud所说,你应该在你的行动中加入Intent.ACTION_MAINIntent.CATEGORY_LAUNCHER ,如下所示

Intent intent = new Intent(this, MainActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

That's all. 就这样。 With this way, if the app is open and when you click the notification, onNewIntent(..) method will trigger but whatever happens, if the app is close, when you click the notification, notification intent will go to the onCreate(..) method of current Activity. 通过这种方式,如果应用程序处于打开状态,当您单击通知时,onNewIntent(..)方法将触发,但无论发生什么,如果应用程序关闭,当您单击通知时,通知意图将转到onCreate(.. )当前活动的方法。

After long inspection i noticed that Android 4.4 (Kitkat and maybe lower) won't call onNewIntent and onResume neither if you use FLAG_UPDATE_CURRENT on pendingIntent. 经过长时间的检查,我注意到Android 4.4(Kitkat可能更低)不会在pendingIntent上使用FLAG_UPDATE_CURRENT时调用onNewIntent和onResume。 Once you change it to FLAG_ONE_SHOT it will start working. 将其更改为FLAG_ONE_SHOT后,它将开始工作。 on Android 6 (and probably also 5) however onNewIntent works even with FLAG_UPDATE_CURRENT flag. 在Android 6上(可能还有5个)但onNewIntent甚至可以使用FLAG_UPDATE_CURRENT标志。

However, it seems that if you create multiple pending intents (eg after receiving two notifications) the data will not be updated with flag FLAG_ONE_SHOT, so flag FLAG_CANCEL_CURRENT may be a better choice (it has no issue with not updated data) 但是,似乎如果您创建多个挂起的意图(例如,在收到两个通知之后),数据将不会使用标志FLAG_ONE_SHOT进行更新,因此标志FLAG_CANCEL_CURRENT可能是更好的选择(它没有更新数据的问题)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android选项卡中没有调用onNewIntent() - onNewIntent() is not called in android Tabs onNewIntent 永远不会被调用 - Android - onNewIntent is never gets called - Android Android - onNewIntent未被调用 - singletop活动 - Android - onNewIntent is not called - singletop activities 在onNewIntent之后未调用Android onResume - Android onResume not called after onNewIntent android选项卡中未调用Linkedin集成onNewIntent() - Linkedin integration onNewIntent() is not called in android Tabs onNewIntent 没有被调用 - onNewIntent is not called 如果未启用android Beam,则不会为android nfc实现调用onNewIntent - onNewIntent is not being called for android nfc implementation without enabling android beam Android SearchView 既未启动可搜索活动,也未调用 onNewIntent() - Android SearchView neither searchable activity launched nor onNewIntent () called 调用onNewIntent()时如何检测我的Android Activity是否已在运行? - How to detect if my Android Activity was already running when onNewIntent() is called? Android 导航组件与深度链接:onNewIntent 调用多次 - Android navigation components with deep link: onNewIntent called multiple times
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM