Android OnNewIntent not called

后端 未结 3 1813
旧巷少年郎
旧巷少年郎 2020-12-10 04:48

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 OnNewInten

3条回答
  •  独厮守ぢ
    2020-12-10 04:57

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

提交回复
热议问题