2

I'm an android newbie, so please be patient!

Finally, I got my XML result from a simple WCF REST API via GCM wishing to build a simple notification with its payload.

@Override
protected void onMessage(Context arg0, Intent arg1) {

    String message = arg1.getStringExtra("payload");
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    Notification note = new Notification();
    note.tickerText=message;
    note.when=System.currentTimeMillis();
    note.defaults |= Notification.DEFAULT_ALL;

    notificationManager.notify(new Random().nextInt(), note);

}

I'm hearing my default notification sound but my bar isn't showing anything, What I'm missing?


EDIT I

Note:

<uses-sdk
    android:minSdkVersion="14"
    android:targetSdkVersion="17" /> 

I tried Jeeter suggestion about building the notification up with the builder, however a MINIMUM API 16 is required which isn't good + my Galaxy note test device is 4.0.4 which is API 15!

@Override
protected void onMessage(Context arg0, Intent arg1) {

    String Message = arg1.getStringExtra("payload");
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    notificationManager.notify(
            new Random().nextInt(),
            new Notification.Builder(this)
                    .setContentTitle(Message)
                    .setContentText(Message).build());

}

public Notification build ()
Added in API level 16
Combine all of the options that have been set and return a new Notification object.


EDIT II

I tried A--C suggestion about using NotificationCompat builder.

@Override
protected void onMessage(Context arg0, Intent arg1) {

    String Message = arg1.getStringExtra("payload");
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            notificationManager.notify(
            new Random().nextInt(),
            new NotificationCompat.Builder(this).setContentTitle("Message")
                    .setWhen(System.currentTimeMillis())
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setContentText(Message).build());

}

No errors but nothing changed, I still hear sound with no appearance, however the message reached the onMessage method.

Ahmed Ghoneim
  • 6,834
  • 9
  • 49
  • 79
  • you need to build it using the Notification's `Builder`. More info here: http://developer.android.com/reference/android/app/Notification.Builder.html – Jeeter Jan 27 '13 at 02:38
  • @Jeeter I tried to build it, but it says that the MINIMUM API must be 16, however my Galaxy Note device is 4.0.4 which it API 15 – Ahmed Ghoneim Jan 27 '13 at 02:40
  • That's weird, because when you go to the site above, and set it to api level 11, Notification.Builder is still allowed. – Jeeter Jan 27 '13 at 02:44
  • @Jeeter I post EDIT I about your suggestion, which you could try it yourself to see the error. – Ahmed Ghoneim Jan 27 '13 at 02:46
  • You need `NotificationCompat.Builder` note the different enclosing class. – A--C Jan 27 '13 at 02:56
  • 1
    @AhmedGhoneim I like your use of edit I,II, etc :) Back on topic: I think you also need to set a **small icon** for the Notification. See [this](http://www.sourcetricks.com/index.php/category/android-status-bar-notifications/) for a source example. Also, [here](http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html) is the doc for the class, for reference. – A--C Jan 27 '13 at 03:10
  • 1
    @A--C Thanks, Ops! It was about adding a small icon! when I added it it works! thank you and please add it as an answer :) – Ahmed Ghoneim Jan 27 '13 at 03:15
  • @AhmedGhoneim Posted the answer, you should accept it since it did resolve the issue you had :-) – A--C Feb 12 '13 at 00:13

1 Answers1

1

Android requires a small icon to be set for it to actually show your status bar notification.

From the documentation, these are the requirements:

A Notification object must contain the following:

• A small icon, set by setSmallIcon()

• A title, set by setContentTitle()

• Detail text, set by setContentText()

Additionally, older platforms (Gingerbread and lower) require a PendingIntent (referred to as the content Intent in the documentation) passed for the Notification to show. This is a limitation of the platform(s).

Depending on how you show the Notification, by not setting a content Intent, the app will crash and the stack trace will be very clear about the reason why.

Using NotificationCompat.Builder, it is easy to set a content Intent via NotificationCompat.Builder#setContentIntent()

Community
  • 1
  • 1
A--C
  • 36,351
  • 10
  • 106
  • 92
  • I am not setting contentIntent() because i don't need it.. I just want to cancel the notification once its clicked. so NotificationCompact won't run on GB? – moDev Mar 12 '13 at 20:04
  • @Mitesh I suggest you make another question - my answer to this question doesn't seem related to your issue. Make sure to include any stack traces if the app crashes. – A--C Mar 12 '13 at 20:06
  • It's related to the current question. Notification is not appearing on GB, but works in JB. No crash issue. I have included the three main points that you have pointed in the answer. – moDev Mar 12 '13 at 20:08
  • @Mitesh I still suggest making a new question, you will need to provide code, which doesn't format well in comments. I use `NotificationCompat.Builder` in my app and it runs fine from 2.2 onwards. – A--C Mar 12 '13 at 20:09
  • http://stackoverflow.com/questions/15371200/notification-doesnt-show-in-ginger-beard-but-works-in-jellybean – moDev Mar 12 '13 at 20:14