Android 在通知 Intent 上添加额外内容

标签 android android-intent android-service android-notifications

我在服务类上使用 putExtras 时遇到问题。首先,我有一个服务,可以接收新消息并创建新通知

MyService.class

public void showMessage(String jsonStr)
{



        Intent notiIntent = new Intent(App.getContext(),MainActivity.class);
        notiIntent.putExtra("message-info", "info text");

        notiIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pintent = PendingIntent.getActivity(App.getContext(), 0, notiIntent,0);

NotificationCompat.Builder notificationBuilder =
                        new NotificationCompat.Builder(this)
                    .setAutoCancel(true)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentIntent(pintent);

NotificationManager mNotificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                    notificationBuilder.setTicker("New Message");
notificationBuilder.setContentTitle("This is the title").setContentText("Message...");

mNotificationManager.notify("myApp", 1234, notificationBuilder.build());

}

MainActivity.class

@Override
public void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);
    setIntent(intent);

    Intent temp = intent;
    Bundle extras = temp.getExtras();
    System.out.println("Log extra new intent: "+temp.getStringExtra("message-info"));

    if(extras != null)
    {
        System.out.println("Log extra new intent HAS EXTRA: "+temp.getStringExtra("message-info"));
    }
}

我只得到空值。 App.getContext() 引用了从 Application 扩展的我的 App 类。

为什么 MyActivity 没有收到任何额外费用?

最佳答案

I only get null values.

您可能已经有该组件的 PendingIntent,并且它没有额外的内容。

如果您想要更新可能存在的 PendingIntent,请将 FLAG_UPDATE_CURRENT 添加到您的 getActivity() 调用中。

关于Android 在通知 Intent 上添加额外内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27383084/

相关文章:

android - SurfaceView 和 ANativeWindow

android - 为什么 realm 包含所有 CPI 架构的 .so 库,即使启用了拆分?

android - 如何在没有 Intent 的情况下从相机拍摄照片

java - 以编程方式在分屏中打开应用程序

java - 进行 whatsapp 视频通话

android - 启动服务在进程终止后重新创建

Android 服务 mysql - 检查数据库更改

android - 无法更新当前行 ListView ,我的代码在下面给出

android - 如何退出 adb logcat

android - Android Services 与 MVVM 中的 Activity 通信(Model View View Model)