Android OnNewIntent not called

强颜欢笑 提交于 2019-11-27 03:19:03

问题


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

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

This is the OnNewIntent method in my MainMenuActivity:

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

    ...
}

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.


回答1:


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



回答2:


As first, you should add android:launchMode="singleTop" to your activity definition in manifest file like below

<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

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.




回答3:


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. Once you change it to FLAG_ONE_SHOT it will start working. on Android 6 (and probably also 5) however onNewIntent works even with FLAG_UPDATE_CURRENT flag.

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)



来源:https://stackoverflow.com/questions/30708462/android-onnewintent-not-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!