获取通知标题Android

时间:2017-03-24 17:14:44

标签: java android notifications

我如何获得通知的通知标题?

这是我的代码:

- 来自通知服务

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

- 来自StartNAFromNS:

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

当只有1个通知执行此操作时,我会得到正确的标题。但是,如果我的应用程序发送了2个通知,我将获得第二个通知的标题。

我怎样才能获得正确的通知标题?

3 个答案:

答案 0 :(得分:4)

通过在我们的课程中扩展NotificationListenerService并使用其onNotificationPosted方法,我们将能够获得通知标题,文字和包名称。使用通知包,我们会获得其应用图标,应用名称等等。

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

答案 1 :(得分: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);

希望这会有所帮助!

答案 2 :(得分: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);
}

//以下方法生成通知并显示通知

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
}