Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NotificationCompat.Builder on API < 11 not working

public static final String NOTIFICATION_TITLE = "Title";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;

private void sendNotification(String msg) {
    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0);

    NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle(NOTIFICATION_TITLE)
            .setStyle(new NotificationCompat.BigTextStyle()
                .bigText(msg))
            .setAutoCancel(true)
            .setContentIntent(contentIntent)
            .setContentText(msg);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}

The above code works smothly on my Nexus 4 (Android 4.3) but my old Nexus One (Android 2.3.7) has still to show me a notification.

Manifest file

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />

<!-- GCM -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="my.package.matchtracker.permission.C2D_MESSAGE" />
<uses-permission android:name="my.package.permission.C2D_MESSAGE" />
<permission
    android:name="my.package.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />
<application
    android:name=".AppClass"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver
        android:name=".GcmBroadcastReceiver"
        android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            <category android:name="my.package" />
        </intent-filter>
        <intent-filter>
            <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
            <category android:name="my.package" />
        </intent-filter>
    </receiver>
    <service android:name=".GcmIntentService" />

    <activity
        android:name="my.package.MainActivity"
        android:screenOrientation="portrait"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

My Scenario

  • Android Studio 0.2.8 under Ubuntu
  • Android SDK Tools 22.2.1
  • Android SDK Platform-tools 18.0.1
  • Android SDK Build Tools 17
  • Android Support Repository 2
  • Android Support Library 18
  • Google Play Services 12

My build-gradle file in my project

dependencies {
    compile 'com.android.support:support-v4:18.0.+', 'com.google.android.gms:play-services:3.1.+'
}

What have I done until now

  • I am aware about Gingerbread needing PendingIntent that's why .setContentIntent is set
  • I have also tried to make a function called on if(Build.VERSION.SDK_INT < 11)
  • Even the old (but deprecated) Notification does not work either
  • The server works as expected because 4.3 devices receive the notifications perfectly

So, right now I have ran out of new ideas, anyone can tell me any other option why the notifications don't work on API 10 ?

like image 342
Eloi Navarro Avatar asked Nov 11 '22 21:11

Eloi Navarro


1 Answers

I have same issue with you, apparent I just notice that effect only Gingerbread version

this is a fix worked for me Android : Notification not working on 2.3.6 (Samsung galaxy y)

like image 178
zinuzoid Avatar answered Nov 15 '22 01:11

zinuzoid