Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取通知标题Android_Java_Android_Notifications - Fatal编程技术网

Java 获取通知标题Android

Java 获取通知标题Android,java,android,notifications,Java,Android,Notifications,如何获取通知的通知标题 这是我的密码: -来自通知服务: resultIntent= new Intent(NotificationService.this, StartNAFromNS.class); resultIntent.putExtra(Intent.EXTRA_TITLE, underestood_name.replace("__", " ")); -来自StartAfromns: String text = this.getInte

如何获取通知的通知标题

这是我的密码:

-来自通知服务

resultIntent= new Intent(NotificationService.this, StartNAFromNS.class);
                        resultIntent.putExtra(Intent.EXTRA_TITLE, underestood_name.replace("__", " "));
-来自StartAfromns:

String text = this.getIntent().getStringExtra(Intent.EXTRA_TITLE);
当只使用一个通知执行此操作时,我会得到正确的标题。但是,如果我的应用程序发送2个通知,我将获得第二个通知的标题


如何获得正确的通知标题?

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

通过扩展
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
的通知 而尚未取消的应用程序,将替换为 最新资料

如果您使用的是
pendingent.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);

希望这会有帮助

但我如何阅读刚刚关闭的通知(以及启动意向的通知)?因为此代码不允许我识别刚刚单击的通知的标题检查此文档帮助解决您的查询()许多应用程序有多个启动活动并共享同一个包。如何从侦听器中找到负责此通知的活动?嗨,如果我想在此通知中添加一些额外的内容,我需要什么属性?感谢问题是,如果我不止一次使用sendNotification(),额外的内容会被最后一条消息覆盖,因此我无法获得第一条通知的标题。使用不同通知的不同请求代码解决了我的问题。谢谢我的荣幸:)。。。如果我的回答似乎有用,那么请投赞成票。
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);