我想为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 :(得分:0)
此方法使用NotificationManager.
支持所有API中的通知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());
}
}
这是一个如何创建加载Pin活动的通知的示例。
createNotification(Calendar.getInstance().getTimeInMillis()," my app","click to load Pin.class","",getApplicationContext());
是的,您可以在通知中添加声音,这是另一个例子:
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);
}