2016-08-05 12 views
1

こんにちは私は次のように動作するようにします:既存の通知を更新する方法はありますか?

私のアプリケーションからの通知は1つありませんが、すでに作成されているはずですが、トレイに通知がある場合は通知のデータを更新したい。

私は現在の通知を受け取ることができますか、これを行う方法は他にありますか?

また、特定のイベントの発生時に頻繁にトリガーされる通知用の単一のタイプのUI +データがあります。

アプリケーションが5分後に一致のスコアに関するメッセージを受信して​​いる状況では、最新の受信メッセージに基づいて現在の通知を更新する必要があります。スコア更新メッセージごとに通知するべきではありません。次のように

私のコードは次のとおりです。

Intent intent = new Intent(this, MainActivity.class); 
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
     Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     intent.putExtra(Constants.SOME_DATA, someData); 

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent, 
     PendingIntent.FLAG_UPDATE_CURRENT); 

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
     .setSmallIcon(R.drawable.icon) 
     .setContentTitle(title) 
     .setContentText(messageBody) 
     .setAutoCancel(true) 
     .setSound(defaultSoundUri) 
     .setContentIntent(pendingIntent); 

NotificationManager notificationManager = 
     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 

答えて

0

あなたがpermanetlyあなたの単一の通知を実行し、その通知を更新するには、startForeground(NOTIFICATION_ID, notification); を使用してフォアグラウンド通知を作成するために持っているよりもあなたが同じNOTIFICATION_ID内を使用して通知を更新することができるよりもしたい場合通知のID * /またはあなたの通知を同じNOTIFICATION_IDで更新し、保留中の意図を維持する場合は

0

セットフラグ.setOngoing(true)と常に同じものを使用しますnotification id.はこの場合、0.です。現在の通知が更新されます。ここにいくつかの問題のコメントを持っている場合

public class MainActivity extends Activity { 

    private ImageView ImageButton; 
    int value; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ImageButton = (ImageView) findViewById(R.id.imageButton); 
     ImageButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       value++; 
       UpdateNow(value + ""); 
      } 
     }); 
    } 

    void UpdateNow(String msg) { 
     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle("asd") 
       .setContentText(msg) 
       .setSound(defaultSoundUri) 
       .setOngoing(true) 
       .setContentIntent(null); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0 /* ID of notification */, notificationBuilder.build()); 
    } 
} 
+0

@Harshシャーこのデモを試してみてください。 –

+0

@Shhali Zahidさんは返信してくれてありがとうございました。私はあなたが示唆したことを試みましたが、まだそれは新しい通知を生成しています。注:通知が更新されると、新しい通知が生成されます。 –

+0

@HarshShah「いつ閉じたのですか」とはどういう意味ですか? –

関連する問題