Android为旧设备提供了带有Ticker的提示通知
下面是如何为有能力的设备发出提示通知,并为较旧的设备使用Ticker。
//点击通知将打开MainActivity Intent i = new Intent(this, MainActivity.class); //稍后使用的操作 //定义为应用程序常量: // public static final String MESSAGE_CONSTANT = "com.example.myapp.notification"; i.setAction(MainActivity.MESSAGE_CONSTANT); // you can use extras as well i.putExtra("some_extra", "testValue"); i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP); PendingIntent notificationIntent = PendingIntent.getActivity(this, 999, i, PendingIntent.FLAG_UPDATE_CURRENT); NotificationCompat.Builder builder = new NotificationCompat.Builder(this.getApplicationContext()); builder.setContentIntent(notificationIntent); builder.setAutoCancel(true); builder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), android.R.drawable.ic_menu_view)); builder.setSmallIcon(android.R.drawable.ic_dialog_map); builder.setContentText("Test Message Text"); builder.setTicker("Test Ticker Text"); builder.setContentTitle("Test Message Title"); //为提示通知设置高优先级 builder.setPriority(NotificationCompat.PRIORITY_HIGH); builder.setVisibility(NotificationCompat.VISIBILITY_PUBLIC); //除非有声音,否则不会显示"Heads Up if (Build.VERSION.SDK_INT >= 21) builder.setVibrate(new long[0]); NotificationManager mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); mNotificationManager.notify(999, builder.build());