Android在通知意图上加了额外的东西

时间:2014-12-09 15:47:30

标签: 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没有收到任何额外内容?

3 个答案:

答案 0 :(得分:2)

  

我只获得空值。

你可能已经为该组件提供了PendingIntent,并且没有额外的内容。

如果您想更新可能存在的PendingIntent,请在FLAG_UPDATE_CURRENT来电中添加getActivity()

答案 1 :(得分:2)

我遇到了同样的问题!我收到了请求代码通知代码:

stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(**post_id**,PendingIntent.FLAG_UPDATE_CURRENT);

mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) c.getSystemService(Context.NOTIFICATION_SERVICE);

mNotificationManager.notify(**post_id**, mBuilder.build());

答案 2 :(得分:0)

就我而言,删除是可行的,

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