【问题标题】:Notification setFullScreenActivity通知集全屏活动
【发布时间】:2012-08-24 18:38:03
【问题描述】:

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

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 ); 

声音和振动很好,因此通知成功通知,但从未创建 MyActivity,即使它是此通知的 FullScreenIntent。

【问题讨论】:

    标签: android android-notifications


    【解决方案1】:

    由于 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); 
    

    【讨论】:

    • 测试了建议的方法:我仍然收到通知,它现在存储在通知栏中。但是,由于某种原因,未启动全屏 Intent...
    • 如果您在三星设备上进行测试,请尝试重新启动它
    • 如果您的手机被锁定,将启动全屏意图。
    【解决方案2】:

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

    查看此链接了解更多详情:http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html

    【讨论】:

      【解决方案3】:

      你需要以下三个变量来传递:

      1. 颜色
      2. 如果打开 LED 是打开的
      3. 如果关闭 LED 则打开

      如果您启用 2 和 3,您的 LED 将闪烁,如果您禁用 2 和 3,则 LED 将关闭

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多