Push notifications service for Android 4.4

后端 未结 2 1129
遇见更好的自我
遇见更好的自我 2021-01-15 06:07

I have done work on push notifications and its working correct but in case till api level 4.0. But notification click does not open the activity in case of api 4.4....I am n

2条回答
  •  太阳男子
    2021-01-15 06:30

    This is working for my apps... Try this...
    
    private void showNotification(Context context) {
            // TODO AK-generated method stub
            String appName = context.getString(R.string.app_name);
            NotificationCompat.Builder mBuilder =
                    new NotificationCompat.Builder(context)
                            .setSmallIcon(R.drawable.notification_icon)
                            .setContentTitle(appName)
                            .setContentText(appName);
    
            Uri sound = Uri.parse("android.resource://" + context.getPackageName() + "/raw/" + audioToneName);
            mBuilder.setSound(sound);
            mBuilder.setAutoCancel(true);
            mBuilder.setVibrate(Utility.vibrationPattern);
            // Creates an explicit intent for an Activity in your app
            Intent resultIntent = new Intent(context, RootActivity.class);
            // The stack builder object will contain an artificial back stack for
            // the
            // started Activity.
            // This ensures that navigating backward from the Activity leads out of
            // your application to the Home screen.
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
            // Adds the back stack for the Intent (but not the Intent itself)
            stackBuilder.addParentStack(RootActivity.class);
            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent =
                    stackBuilder.getPendingIntent(
                            0,
                            PendingIntent.FLAG_UPDATE_CURRENT
                            );
            mBuilder.setContentIntent(resultPendingIntent);
            NotificationManager mNotificationManager =
                    (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            // mId allows you to update the notification later on.
            mNotificationManager.notify(321, mBuilder.build());
        }
    

提交回复
热议问题