【问题标题】:Get Notification Title Android获取通知标题 Android
【发布时间】:2017-03-24 17:14:43
【问题描述】:

如何获取通知的通知标题?

这是我的代码:

-来自通知服务

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

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

【问题讨论】:

    标签: java android notifications


    【解决方案1】:

    通过扩展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);
        }
        }
    }
    

    【讨论】:

    • 但是我怎样才能阅读刚刚关闭的通知(并且启动了一个意图)?因为这段代码不允许我识别刚刚点击的通知的标题
    • 查看此文档帮助解决您的问题 (developer.android.com/reference/android/service/notification/…)
    • 许多应用程序有多个启动活动并共享同一个包。如何从侦听器中找到负责此通知的活动?
    • 嗨,如果我想在此通知中添加一些额外的内容,我需要什么属性?谢谢
    【解决方案2】:

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

    希望这会有所帮助!

    【讨论】:

    • 对不同的通知使用不同的 requestCode 解决了我的问题。谢谢!
    • 我的荣幸 :)... 如果我的回答看起来很有用,请点赞。
    【解决方案3】:

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

    【讨论】:

    • 问题是,如果我多次使用 sendNotification(),多余的会被最后一条消息覆盖,所以我无法获得第一个通知的标题
    猜你喜欢
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多