質問

I've searched many times but I was not able to find what I actually needed, There is something on stackoverflow like this and this but I need to implement my code for api level 8 not 11. So how can I implement it in my code?

My code is here:

....

mNotification = new Notification(icon, tickerText, when);

//create the content which is shown in the notification pulldown
mContentTitle = mContext.getString(R.string.noti_comes_t_l); //Full title of the notification in the pull down
CharSequence contentText = "click to see notification"; //Text of the notification in the pull down

//you have to set a PendingIntent on a notification to tell the system what you want it to do when the notification is selected
//I don't want to use this here so I'm just creating a blank one 
mContentIntent  = PendingIntent.getActivity(mContext, 0, new Intent(mContext, notify.class), 0); 

//add the additional content and intent to the notification
mNotification.setLatestEventInfo(mContext, mContentTitle, contentText, mContentIntent);

//make this notification appear in the 'Ongoing events' section
mNotification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL ;

//show the notification
mNotificationManager.notify(NOTIFICATION_ID, mNotification);

....

How can I implement this?

役に立ちましたか?

解決

I think you need NotificationCompat from the Support library, you can use this code to generate a notification, adapt it as you need

    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);

    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(context)
    .setSmallIcon(icon)
    .setContentTitle(context.getString(R.string.app_name))
    .setContentText(message)
            .setDeleteIntent(PendingIntent);

    mBuilder.setWhen(when);

    Intent notificationIntent = new Intent(context, YOURCLASS.class);      
    notificationIntent.putExtra("url", url);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent intent = PendingIntent.getActivity(context, random, notificationIntent, PendingIntent.FLAG_ONE_SHOT);
    mBuilder.setContentIntent(intent);
    Notification notification = mBuilder.build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    // Vibrate if vibrate is enabled
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    notificationManager.notify(random, notification);   
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top