2014-02-19 22 views
0

創建通知我想創建一個通知API 10.這是我的函數:爲API 10

public void showNotificationCombat(Context context) { 


    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
      new Intent(context, Pin.class), 0); 

    Notification noti = 
      new NotificationCompat.Builder(context) 
      .setSmallIcon(R.drawable.icon) 
      .setContentTitle("My notification") 
      .setContentText("Hello World!") 
      .setContentIntent(contentIntent).build(); 

    noti.flags |= Notification.FLAG_AUTO_CANCEL; 

    NotificationManager mNotificationManager = 
      (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(0, noti); 
}  

我沒有在Eclipse中的任何錯誤,但我的應用程序崩潰在這一點上...我不知道爲什麼..一切都看起來不錯!

回答

0

這種方法支持在所有API's通知,使用NotificationManager.

public void createNotification(long when, String notificationTitle, String notificationContent, String notificationUrl, Context ctx){ 
    try{ 
     Intent notificationIntent; 
     if("".equals(notificationTitle)){ 
      notificationTitle = ctx.getResources().getString(R.string.app_name); 
     } 
     /*large icon for notification,normally use App icon*/ 
     Bitmap largeIcon = BitmapFactory.decodeResource(ctx.getResources(),R.drawable.ic_launcher); 
     int smalIcon =R.drawable.ic_launcher; 
     /*create intent for show notification details when user clicks notification*/ 
     if(!"".equals(notificationUrl)){ 
      notificationIntent = new Intent(Intent.ACTION_VIEW ,Uri.parse(notificationUrl));  
     } 
     else{ 
      //Intent to load Pin.class 
      notificationIntent = new Intent(this, Pin.class); 
     }   
     /*create new task for each notification with pending intent so we set Intent.FLAG_ACTIVITY_NEW_TASK */ 
     PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK); 

     /*get the system service that manage notification NotificationManager*/ 
     NotificationManager notificationManager =(NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); 

     /*build the notification*/ 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(ctx) 
     .setWhen(when) 
     .setContentText(notificationContent) 
     .setContentTitle(notificationTitle) 
     .setSmallIcon(smalIcon) 
     .setAutoCancel(true) 
     .setTicker(notificationTitle) 
     .setLargeIcon(largeIcon)   
     .setContentIntent(pendingIntent); 

     /*sending notification to system.Here we use unique id (when)for making different each notification 
     * if we use same id,then first notification replace by the last notification*/ 
     notificationManager.notify((int) when, notificationBuilder.build()); 
    }catch(Exception e){ 
     Log.e("NotificationManager", "createNotification::" + e.getMessage()); 
    } 

} 

這是一個例子如何創建的通知加載銷活動。

createNotification(Calendar.getInstance().getTimeInMillis()," my app","click to load Pin.class","",getApplicationContext()); 

是的,你可以將聲音添加到您的通知,here's另一個例子:

private void displayNotification(String msg) 
{ 
    Context context = getApplicationContext(); 
    int ID = 12; 
    Intent i = new Intent(context, Pin.class); 
    i.putExtra("ID", ID); 
    i.putExtra("msg",msg); 
    PendingIntent pendInt = PendingIntent.getActivity(context, 0, i, 0); 
    Notification notif = new Notification(0,"Hey!, Jorgesys you have a new new Message",System.currentTimeMillis()); 
    NotificationManager nm = (NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE); 
    notif.setLatestEventInfo(context, "Message", msg, pendInt);  
    notif.flags = Notification.FLAG_AUTO_CANCEL; 
    notif.icon = R.drawable.ic_launcher;  
    notif.defaults |= Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;   
    notif.ledARGB = Color.WHITE; 
    notif.flags |= Notification.FLAG_SHOW_LIGHTS; 
    notif.ledOnMS = 5000;       
    notif.ledOffMS = 5000;  
    nm.notify(ID, notif); 
} 
+0

什麼是notificationUrl參數?當用戶點擊通知進入Pin.class活動時,我想要...我會對notificationUrl施加什麼值? –

+0

使用空字符串(「」),但如果要在點擊通知時打開Url,請定義一個url。 – Jorgesys

+0

當用戶點擊通知來運行Activity Pin.class時,我必須添加代碼(以及哪些代碼)?確定! –