Question

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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top