Android : Notification not working on 2.3.6 (Samsu

2019-01-26 20:17发布

问题:

The following code has been confirmed to work fine on devices running HONEYCOMB+. However on Samsung galaxy Y it is not producing any notifications.

        String tickerText = userString + " Download Queued";
        Notification notification =  new NotificationCompat.Builder(this).setAutoCancel(true)
                                                .setContentTitle(userString)
                                                .setContentText("Queued")
                                                .setSmallIcon(R.drawable.stat_sys_download_done)
                                                .setWhen(System.currentTimeMillis())
                                                .setTicker(tickerText)
                                                .build();
        if(DBG_ENABLE) {
            LogUtils.logD(TAG_LOG, "Posting queue notification : " + 0);
        }
        NotificationManager notificationManager =
                (NotificationManager) getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
        notificationManager.notify(0, notification);

Note :

  • I see the "Posting Queue notification" in the logs.
  • I have copied the drawable stat_sys_download_done from android sdk into my project.

I'm not able to think of a way to debug this problem. I'm not sure if there is anything I'm missing. Any suggestions to fix this is appreciated.

回答1:

As CommonsWare suggested, I ran the app on a 2.3 emulator and it crashed. Reason being ContentIntent was not set. GingerBread expects a ContentIntent. So I added a dummy pending intent like :

            PendingIntent pi = PendingIntent.getBroadcast(this, 0, new Intent(), PendingIntent.FLAG_UPDATE_CURRENT);
            Notification notification =  new NotificationCompat.Builder(this).setAutoCancel(true)
                                            .setContentTitle(userString)
                                            .setContentText("Queued")
                                            .setContentIntent(pi)
                                            .setSmallIcon(R.drawable.stat_sys_download_done)
                                            .setWhen(System.currentTimeMillis())
                                            .setTicker(tickerText)
                                            .build();


回答2:

y0u can do either this also.it works on 2.3 and 2.3+

 Notification notification = new NotificationCompat.Builder(this)
        .setTicker("new notification")
        .setContentTitle("title")
        .setContentText("hello").setSmallIcon(R.drawable.ic_launcher)
        .setContentIntent(pendingIntent)
    .addAction(android.R.drawable.ic_media_play, "play",pendingIntent).build();


回答3:

When I tried the above solution, it crashed the app just declaring the empty PendingIntent as shown. Deciding I had a scope problem, because I am coding this notification in a BroadcastListener, I moved the pending intent into the onReceive method of the listener, and used the incoming context and intent instead of dummys. That works perfectly in my 2.3.6 Galaxy phone. Here is my code:

BroadcastReceiver sitInReceiver = new BroadcastReceiver() {
        @Override
            public void onReceive(Context context, Intent intent) {
                String target = intent.getStringExtra("target");
                ....
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            notifB.setContentIntent(pi);

            NotificationManager mNotificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            int nmId=1;
                // mId allows you to rewrite the same the notification or make a different one.
            mNotificationManager.notify(nmId, notifB.build());
        }

Note that I declared the builder, notifB, outside the listener where all the non-dynamic data is declared:

NotificationCompat.Builder notifB = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.ic_launcher) 
    .setAutoCancel(true);