10

次のコードがあります。これは、通知中に新しいアクティビティを初期化することを想定しています。これはサービス クラスにあります。

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

音とバイブレーションがうまく動くので通知は成功しますが、この通知の FullScreenIntent であるにも関わらず MyActivity は作成されません。

4

3 に答える 3

0

異なるバージョン間で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); 
于 2012-08-24T21:47:39.250 に答える
0

Notification.BuilderAPI 11 以降が必要です。サポートされているのは API 4+ のみを必要とする代わりに使用NotificationCompat.Builderし、サポートされているデバイスに新しい機能 (つまり、Jelly Bean で利用可能な新しい通知スタイル) を実装できますが、古いデバイスは新しい機能を無視します。それはすべてをよりスムーズにします。

詳細については、次のリンクを参照してください: http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html

于 2013-02-25T09:03:05.720 に答える