這種方法支持在所有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);
}
什麼是notificationUrl參數?當用戶點擊通知進入Pin.class活動時,我想要...我會對notificationUrl施加什麼值? –
使用空字符串(「」),但如果要在點擊通知時打開Url,請定義一個url。 – Jorgesys
當用戶點擊通知來運行Activity Pin.class時,我必須添加代碼(以及哪些代碼)?確定! –