简体   繁体   English

如何从通知中打开片段

[英]How to open a fragment from a notification

I'm writing a messaging app that makes use of fragments for various views. 我正在编写一个消息传递应用程序,它利用各种视图的片段。 On the first fragment that is open, the user is presented with a list of contacts, and when he clicks on a contact, another fragment opens to show the existing chat window. 在打开的第一个片段上,向用户显示联系人列表,当他点击联系人时,将打开另一个片段以显示现有的聊天窗口。 This fragment is started with the following code: 此片段使用以下代码启动:

FragmentManager fragmentManager = getFragmentManager();
msgChat msgChatFragment = new msgChat();
Bundle bundle = new Bundle(); 
bundle.putString("userSelected", manageContact.getName());
msgChatFragment.setArguments(bundle);
fragmentManager.beginTransaction().replace(R.id.content_frame, msgChatFragment).addToBackStack(null).commit();
getActivity().setTitle("Message Chat");

I also have a notification to inform the user of incoming messages. 我还有一个通知,告知用户传入的消息。 However, I'm having trouble launching the intended chat fragment when the user taps on the notification. 但是,当用户点击通知时,我无法启动预期的聊天片段。 My notification code is as follows: 我的通知代码如下:

Intent intent = new Intent(context, MainActivity.class);
String notificationMessage = "My notification message";
intent.setAction("Action1");
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder noti = new NotificationCompat.Builder(
  context).setContentTitle("My App")
  .setContentText(notificationMessage)
  .setSmallIcon(R.drawable.ic_stat_notify)
  .setContentIntent(pIntent).setAutoCancel(true)
  .setDefaults(Notification.DEFAULT_ALL);
notificationManager.notify(notifyId, noti.build());

In my MainActivity's onResume(), I have the following code to catch the intent: 在我的MainActivity的onResume()中,我有以下代码来捕获意图:

Intent intent = getIntent();
try {
  String action = intent.getAction().toUpperCase();
}catch(Exception e){
  Log.e(TAG, "Problem consuming action from intent", e);              
}

However, getIntent() seems to always return a null value. 但是,getIntent()似乎总是返回一个空值。

What am I doing wrong? 我究竟做错了什么? Thanks! 谢谢!

I think this line is not correct: 我认为这条线不正确:

intent.setAction("Action1");

You should use the following statement to indicate the willing of the intent (ie open an activity): 您应该使用以下语句来表明意图的意愿(即开展活动):

intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

To identify the intent origin you can use putExtra function like that 要识别意图来源,您可以像这样使用putExtra函数

intent.putExtra("origin", "incoming_message__open_chat")

Do not forget to enable receiving that in you activity: 不要忘记在您的活动中启用接收:

<activity android:name="MainActivity">
    <!-- This activity is the main entry, should appear in app launcher -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

In your activity get the "origin" 在你的活动中得到“起源”

protected void onCreate(Bundle savedInstanceState)
{
   ...
   String origin = savedInstanceState.getString("origin");
   ...
}

I have not the full solution but you should deep in this direction and read: http://developer.android.com/guide/components/intents-filters.html 我没有完整的解决方案,但你应该深入这个方向并阅读: http//developer.android.com/guide/components/intents-filters.html

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

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