Ahyox Prodigy
Ahyox Prodigy

Reputation: 27

Notification working on Emulator but not on my device

Im developing an app that runs on API 7 and above so I have to used NotificationCompat.Builder instead of Notification because it deprecated in higher version. This works fine on emulator, but when tested on my device there was no notification. Please can somebody help me. NB: Is it not possible to use just an API for API 7 to 14. I will like to know because my device uses API 7

Upvotes: 1

Views: 332

Answers (1)

matreshkin
matreshkin

Reputation: 2208

try this function - it works on android 2 up to 4 :

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;



    public static void pushNotification(final Context context,
            int icon, String name, String descr, Intent activityIntent) {
        NotificationManager notifyMgr = 
                (NotificationManager)context.getSystemService( 
                Context.NOTIFICATION_SERVICE); 
        long when = System.currentTimeMillis(); 
        PendingIntent pIntent = PendingIntent.getActivity(
                context, 0, activityIntent, 0);
        Notification notification = null;
        if (android.os.Build.VERSION.SDK_INT < 11)
            notification = getNotification8(context, 
                    icon, name, descr, when, pIntent);
        else notification = getNotification11(context, 
                icon, name, descr, when, pIntent);
        notifyMgr.notify(NOTIFY_ID, notification);
    }

    @SuppressWarnings("deprecation")
    private static Notification getNotification8(Context context,
            int icon, String name, String descr, 
            long when, PendingIntent pIntent) {
        Notification notification = new Notification(icon, name, when);
        notification.setLatestEventInfo(context, name, descr, pIntent);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        return notification;
    }   
    @TargetApi(11)
    private static Notification getNotification11(Context context,
            int icon, String name, String descr, 
            long when, PendingIntent pInten) {
        Notification notification = new Notification.Builder(context)
            .setTicker(name)
            .setContentTitle(name)
            .setContentText(descr)
            .setSmallIcon(icon)
            .setContentIntent(pInten)
            .setAutoCancel(true)
            .setWhen(when)
            .getNotification();     
        return notification;
    }

Upvotes: 1

Related Questions