Android 通知setFullScreenActivity

Android 通知setFullScreenActivity,android,android-notifications,Android,Android Notifications,我有下面的代码,假设在通知期间初始化一个新活动,它位于服务类中 Intent push = new Intent(); push.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK ); push.setClass( context, MyActivity.class ); PendingIntent pi = PendingIntent.getActivity( context, 0, push, PendingIntent.FLAG_ONE_SHOT ); lo

我有下面的代码,假设在通知期间初始化一个新活动,它位于服务类中

Intent push = new Intent();
push.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
push.setClass( context, MyActivity.class );
PendingIntent pi = PendingIntent.getActivity( context, 0, push, PendingIntent.FLAG_ONE_SHOT );

long[] vibraPattern = {0, 500, 250, 500 };

Notification noti = new Notification.Builder( getApplicationContext() )
     .setVibrate( vibraPattern )
     .setDefaults( Notification.DEFAULT_SOUND )
     .setFullScreenIntent( pi , true )
     .setContentIntent( pi )
     .getNotification();

notifMng.notify( 0 , noti ); 

声音和振动进行得很好,因此noti被成功通知,但是MyActivity从未被创建,即使它是此通知的全屏意图。

由于API在不同版本之间变化太大,通知很棘手。您的解决方案以API级别11+(3.0.x)为目标,不适用于任何2.x设备。getNotification()方法在4.1中也被弃用

您的通知缺少要显示的内容。它将在收到通知时实际启动活动,但不会显示通知,因为它没有任何要显示的内容

如果要在收到推送后立即启动活动,请添加.setFullScreenIntent(pi,true)

我已修改了您的代码,使其能够正常工作:

Intent push = new Intent();
push.setClass(context, MyActivity.class);
PendingIntent pi = PendingIntent.getActivity(context, 0, push, 
                    PendingIntent.FLAG_ONE_SHOT);

long[] vibraPattern = { 0, 500, 250, 500 };

Notification noti = new Notification.Builder(getApplicationContext())
            .setVibrate(vibraPattern)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setContentIntent(pi)
            .setContentTitle("Title")
            .setContentText("content text")
            .setSmallIcon(R.drawable.ic_launcher)
            .getNotification();

    NotificationManager notificationManager = 
        (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
         notificationManager.notify(0, noti); 

Notification.Builder
需要API 11+。改为使用
NotificationCompat.Builder
,它只需要API 4+即可获得支持,并允许在支持的设备上实现新功能(即Jelly Bean中提供的新通知样式),而旧设备只会忽略新功能。它使一切变得更加顺利


有关更多详细信息,请参阅此链接:

您需要以下三个变量才能传递:

  • 颜色
  • 如果打开指示灯,则指示灯亮起
  • 如果关闭指示灯,则指示灯亮起
  • 如果启用2和3,led将闪烁,如果禁用2和3,led将关闭

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context );
    mBuilder.setLights(Color.RED, 1, 1); // will blink
    

    测试了建议的方法:我仍然收到通知,它现在存储在通知栏中。但是,由于某些原因,全屏意图未启动…如果您在三星设备上进行测试,请尝试重新启动它。我在S4上遇到了此问题。只有全屏意图在所有其他设备上工作正常。如果您的手机被锁定,全屏意图将启动。