2017-05-12 2 views
0

私のアプリケーションはサービスとして動作していますので、アプリケーションの実行中にユーザに通知する必要があります。通知バーに通知を追加しようとしています。それはサービスアプリケーションなので、私は常に通知を表示する必要があります。 アプリケーションを終了しても通知を削除できないようにするにはどうすればよいですか。 も、アプリケーションが終了した通知を表示する方法通知を削除しないようにする - Android

mBuilder.setOngoing(true); 

、使用してスワイプでアプリケーションを削除するには、ユーザーを防ぐために私ができます。私は以下のコードを使用している

private void addNotificaiononNotificationBar(){ 

     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(this) 
         .setSmallIcon(R.mipmap.ic_launcher) 
         .setContentTitle("My notification") 
         .setContentText("Hello World!"); 

     Intent resultIntent = new Intent(this, MainActivity.class); 
// Because clicking the notification opens a new ("special") activity, there's 
// no need to create an artificial back stack. 
     PendingIntent resultPendingIntent = 
       PendingIntent.getActivity(
         this, 
         0, 
         resultIntent, 
         PendingIntent.FLAG_UPDATE_CURRENT 
       ); 

     mBuilder.setContentIntent(resultPendingIntent); 
     mBuilder.setOngoing(true); 
     // Sets an ID for the notification 
     int mNotificationId = 001; 
     // Gets an instance of the NotificationManager service 
     NotificationManager mNotifyMgr = 
       (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     // Builds the notification and issues it. 
     mNotifyMgr.notify(mNotificationId, mBuilder.build()); 

    } 
+0

フォアグラウンドサービスを開始 –

答えて

2

あなたは通知を削除制限したい場合は、サービスが実行されたときに通知が存在しますForegroundServiceを作成することができます。

+0

私のアプリケーションはバックグラウンドアプリですか?通知バーに通知を追加することは可能です –

+0

バックグラウンドアプリに関係なく、 **前景**は前景アプリと同じ**優先度**を意味します。つまり、可能です。 – DEADMC

関連する問題