2016-03-22 35 views
3

私はこのコードを使用してヘッズアップ通知を作成しています。ヘッドアップ通知を終了し、通常の通知を作成します

private static void showNotificationNew(final Context context,final String title,final String message,final Intent intent, final int notificationId, final boolean isHeaderNotification) { 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context.getApplicationContext()) 
      .setSmallIcon(R.drawable.prime_builder_icon) 
      .setPriority(Notification.PRIORITY_DEFAULT) 
      .setCategory(Notification.CATEGORY_MESSAGE) 
      .setContentTitle(title) 
      .setContentText(message) 
      .setWhen(0) 
      .setTicker(context.getString(R.string.app_name)); 

    PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
    notificationBuilder.setContentText(message); 
    if(isHeaderNotification) { 
     notificationBuilder.setFullScreenIntent(fullScreenPendingIntent, false); 
    } 

    notificationBuilder.setContentIntent(fullScreenPendingIntent); 
    notificationBuilder.setAutoCancel(true); 


    Notification notification = notificationBuilder.build(); 
    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    manager.notify(notificationId, notification); 
} 

事があり、通知は、ユーザーの注意を喚起するために、トップ画面の良い部分を占めて表示されますが、数秒後に、それは却下すべきであると、通常の通知が表示されます。

しかし、このコードではそうしていません。通知がユーザーを却下するまで、すべてのトップ画面を占有し続けます。

Handlerを使用して数秒後に同じIDの別の通常の通知を作成すると思っていますが、これを行うより良い方法があるかどうかを知りたいと思います。

WhatsAppの例に従って、私が望む動作をシミュレートします。

enter image description here enter image description here

答えて

4

あなたがsetFullScreenIntentを使用しているため、問題が発生した:

ステータス バーに通知を掲示するのではなく、起動する意思。 を要求する非常に優先度の高い通知で使用する場合のみ、着信通話またはユーザーが明示的に特定の時間に設定した 目覚まし時計などのユーザーの注意が必要です。 この機能を他の目的で使用している場合は、ユーザーに オプションを指定してオフにし、通常の通知を使用してください。 は非常に混乱する可能性があります。

また、このanswerで説明したように、setVibrateを使用してヘッドアップを行う必要があります。

private static void showNotificationNew(final Context context, final String title, final String message, final Intent intent, final int notificationId) { 
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT); 

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context.getApplicationContext()) 
      .setSmallIcon(R.drawable.small_icon) 
      .setPriority(Notification.PRIORITY_HIGH) 
      .setContentTitle(title) 
      .setContentText(message) 
      .setVibrate(new long[0]) 
      .setContentIntent(pendingIntent) 
      .setAutoCancel(true); 

    NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    manager.notify(notificationId, notificationBuilder.build()); 
} 
+0

恐ろしい:

これはヘッドアップ通知を作業の一例です!ありがとうございました。 – leonvian

+0

フルスクリーンインテントを設定すると、ヘッズアップ通知が自動的に却下されるのを防ぐことに興味があります。私の電話が鳴っていると思うと意味がありますが、何が起こっているのかを追跡するために私はしばらく時間がかかりました。 –

関連する問題