2017-03-24 5 views
3

通知の通知のタイトルを取得するにはどうすればよいですか?通知のタイトルを取得するAndroid

は、ここに私のコードです:

-from通知サービス

resultIntent= new Intent(NotificationService.this, StartNAFromNS.class); 
         resultIntent.putExtra(Intent.EXTRA_TITLE, underestood_name.replace("__", " ")); 

-from StartNAFromNS:

String text = this.getIntent().getStringExtra(Intent.EXTRA_TITLE); 

のみ1通知でこれをやったとき、私が手正しいタイトル。ただし、アプリケーションから2つの通知が送信された場合は、2番目の通知のタイトルが表示されます。

適切な通知のタイトルを取得するにはどうすればよいですか?

答えて

3

通知idは、アプリケーション内で一意である必要があります。

同じidとの通知がすでに アプリケーションによって投稿されていて、まだ解除されていない場合、それは 更新された情報に置き換えられます。

NotificationManager notiManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);  
notiManager.notify(UNIQUE_ID, notification); 

あなたはPendingIntent.getActivity()メソッドを使用している場合は、別の通知に対して異なるrequestCodeを使用します。

Intent resultIntent= new Intent(NotificationService.this, StartNAFromNS.class); 
resultIntent.putExtra(Intent.EXTRA_TITLE, underestood_name.replace("__", " "));  

PendingIntent pI = PendingIntent.getActivity(mContext, REQUEST_CODE, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

希望これは助けます!

+0

さまざまな通知に異なるrequestCodeを使用すると、私の問題が解決しました。ありがとう! –

+0

私の喜び:)...私の答えが役に立つと思われるなら、アップヴォートをお願いします。 – FAT

0

このコードはfcmで正しく動作します。 fcmコンソールまたはサーバーからメッセージとタイトルを送信できます。登録されたモバイルアプリによって受信された通知。

@Override 
public void onMessageReceived (String from, Bundle data) { 
    //Getting the message from the bundle 
long dateTime = data.getLong("google.sent_time"); 
Bundle notificationBundle = data.getBundle("notification"); 
    String message = notificationBundle.getString("body"); 
    String title = notificationBundle.getString("title"); 

    //Displaying a notiffication with the message 
    sendNotification(title, message); 
} 

//以下の方法は、通知を生成しているし、我々は、通知のタイトル、テキストやパッケージを取得することができますNotificationListenerServiceを拡張し、私たちのクラスにそのonNotificationPosted方法を用いることにより、通知

private void sendNotification(String title, String message) { 
    Intent intent = new Intent(this, MainActivity.class); 
    intent.putExtra("message", message); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    int requestCode = 0; 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT); 
    NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this) 
      .setContentTitle(title) 
      .setContentText(message) 
      .setAutoCancel(true) 
      .setContentIntent(pendingIntent); 

    if (Build.VERSION.SDK_INT >= 21) 
     noBuilder.setSmallIcon(R.mipmap.ic_launcher); 
    else 
     noBuilder.setSmallIcon(R.mipmap.ic_launcher_small); 

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.notify(0, noBuilder.build()); //0 = ID of notification 
} 
+0

sendNotification()を複数回使用すると、余分なメッセージが最後のメッセージで上書きされるため、最初の通知のタイトルを取得できません。 –

2

を表示します名。通知パッケージを使用して、アプリアイコン、アプリ名などを取得します。

public class MyNotification extends NotificationListenerService { 
    Context context; 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     context = getApplicationContext(); 
    } 
    @Override 
    public void onNotificationPosted(StatusBarNotification sbn) { 
     // We can read notification while posted. 
    for (StatusBarNotification sbm : MyNotification.this.getActiveNotifications()) { 
      String title = sbm.getNotification().extras.getString("android.title"); 
      String text = sbm.getNotification().extras.getString("android.text"); 
      String package_name = sbm.getPackageName(); 
     Log.v("Notification title is:", title); 
     Log.v("Notification text is:", text); 
     Log.v("Notification Package Name is:", package_name); 
    } 
    } 
} 
+0

しかし、閉鎖された(そしてそれは意図を出した)?このコードでは、私がクリックされたばかりの通知のタイトルを特定することができないため、 –

+0

このドキュメントを参照してクエリを実行してください(https://developer.android.com/reference/android/service/notification/NotificationListenerService)。 html) –

関連する問題