2

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 not able to understand the answer, I have search on KitKat and for notifications it has use Notification.Builder Api,with which it gives the same result.

private void generateNotification(Context context, String message, String id) {
    int icon = R.drawable.app_icon;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);

    Notification notification = new Notification(icon, message, when);
    String title = context.getString(R.string.app_name);

    JSONObject jobj = new JSONObject();

    try {
        jobj.put("id", id);

    } catch (JSONException e) {
        e.printStackTrace();
    }
    System.out.println("json object" + jobj.toString());
    Intent notificationIntent = null;

    notificationIntent = new Intent(context, JamInfo.class);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    notificationIntent.putExtra("longi", longi);
    notificationIntent.putExtra("lati", lati);


    PendingIntent intent = PendingIntent.getActivity(context, 0,
            notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    notification.setLatestEventInfo(context, title, message, intent);


    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notification.defaults |= Notification.DEFAULT_LIGHTS;
     }

    notificationManager.notify((int) System.currentTimeMillis(),
            notification);

}
user3155906
  • 23
  • 1
  • 5
  • 2
    Post your codes so we can help you. – ChuongPham Jan 03 '14 at 05:53
  • i have edit my question, i have post the method that help generating notification and take to the respective activity on click of notification. The above code working correct till API 4.0...but it is not working in API 4.4...I have tested the same on Nexus 4 and Nexus 5 – user3155906 Jan 07 '14 at 05:54
  • user3155906: See my answer below. – ChuongPham Jan 08 '14 at 12:21

2 Answers2

2
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());
    }
Arfan Mirza
  • 668
  • 1
  • 14
  • 24
  • I have question on this, Notification.Builder API works well apps API target version for 4.0...what about if I want to run my below API 4.0 ? or it runs correctly on below versions too... – user3155906 Jan 07 '14 at 06:37
  • Actually, i use android-support-v4.jar and Notification.Builder is supported if you import from support package... It is correctly runs for me till Android-Gingerbread... – Arfan Mirza Jan 07 '14 at 07:07
  • i have android support.v4 jar file in my libs folder already..its till not running and giving Notification.Builder api not found error in console – user3155906 Jan 16 '14 at 05:17
  • Right click on project, AndroidTools and then click AddSupportLibrary. Then you will able to access the library project api. – Arfan Mirza Jan 18 '14 at 06:06
  • if this still not work then you make small change that use Intent.FLAG_ACTIVITY_NEW_TASK instead of PendingIntent.FLAG_UPDATE_CURRENT that will work on kitkat. – Harshvardhan Trivedi Jul 05 '14 at 07:02
1

Try something like below:

        private final static int NOTIFICATION_ID = 1;
        private static NotificationManager mNotificationManager;

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
            // API 16 onwards
            Notification.Builder builder = new Notification.Builder(context);
            builder.setAutoCancel(false)
                .setContentIntent(pendingIntent)
                .setContentText(context.getString(R.string.notification_text))
                .setContentTitle(context.getString(R.string.app_name))
                .setOngoing(true)
                .setSmallIcon(R.drawable.ic_notifier)
                .setWhen(System.currentTimeMillis());
            Notification notification = builder.build();
            notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
            mNotificationManager.notify(NOTIFICATION_ID, notification);         
        } else {
            // API 15 and earlier
            NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
            builder.setAutoCancel(false)
                .setContentIntent(pendingIntent)
                .setContentText(context.getString(R.string.notification_text))
                .setContentTitle(context.getString(R.string.app_name))
                .setOngoing(true)
                .setSmallIcon(R.drawable.ic_notifier)
                .setWhen(System.currentTimeMillis());
            Notification notification = builder.getNotification();
            notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
            mNotificationManager.notify(NOTIFICATION_ID, notification);         
        }

Add the JSON part where appropriate. And don't forget to add the android-support-v4.jar to your project or it won't compile.

ChuongPham
  • 4,761
  • 8
  • 43
  • 53