繁体   English   中英

获取通知标题Android

[英]Get Notification Title Android

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

这是我的代码:

- 从通知服务

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个通知,我将获得第二个通知的标题。

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

通过在我们的类中扩展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);
    }
    }
}

通知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);

希望这会有所帮助!

此代码适用于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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM