未调用 Android OnNewIntent [英] Android OnNewIntent not called

查看:29
本文介绍了未调用 Android OnNewIntent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了几种方法,我尝试了所有方法,但都无法奏效.我不知道为什么它如此复杂,在文档中它看起来如此简单!我想通过通知触发 OnNewIntent(用户在通知栏中点击它).

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).

目前我已将我的 Activity 设置为 singleTop

Currently I have set my Activity as 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());
}

这是我的 MainMenuActivity 中的 OnNewIntent 方法:

This is the OnNewIntent method in my MainMenuActivity:

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

    ...
}

我从来没有接到 OnNewIntent 电话.我不知道我做错了什么.我在整个应用程序中只使用了 2 个活动,并且 MainMenuActivity 出现在 LoginActivity 之后,因此无论如何 MainMenuActivity 应该始终位于堆栈的顶部(我有更多的片段可以在 MainMenuActivity 中替换它们).

I never get the OnNewIntent call. 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).

任何帮助将不胜感激!谢谢各位.

Any help would be appreciated! Thank you guys.

推荐答案

好的,在发布我的问题后很快就可以使用了.我认为我们代码中的主要区别在于我将标志PendingIntent.FLAG_UPDATE_CURRENT"传递给 PendingIntent 对象的创建/检索.这个帖子 帮我解决了这个问题.

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. 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());

这篇关于未调用 Android OnNewIntent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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