【问题标题】:Showing fragments from push notifications显示来自推送通知的片段
【发布时间】:2014-10-29 11:09:13
【问题描述】:

我正在使用推送通知在我的应用中显示片段,但我的实现似乎不稳定

应用运行时和应用未运行时有两种情况

在这两种情况下,我都希望应用显示特定的片段

这是我的清单文件中的 FragmentActivity

    <activity
        android:name=".Main"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        android:theme="@style/Theme.NoBackground"
        android:windowSoftInputMode="adjustPan" >
    </activity>

这是我创建通知的代码,服务在收到 GCM 消息时调用它

private void sendNotification(String message) {

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

    int requestID = (int) System.currentTimeMillis();

    final PendingIntent contentIntent = PendingIntent.getActivity(
            getApplicationContext(), requestID, new Intent(
                    getApplicationContext(), Main.class).setAction(Actions.SHOW_FRIENDS_FRAGMENT), PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
            this)
            .setSmallIcon(R.drawable.notification_icon)
            .setAutoCancel(true)
            .setContentText(message)
            .setSound(
                    RingtoneManager
                            .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(Util.getApp().getNotificationId(), mBuilder.build());
}

在我的活动中,我同时检查了 onCreate 和 onNewIntent

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    checkIntent(intent);
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    checkIntent(intent);
}

private void checkIntent(Intent i){
    try{
        String action = i.getAction();

        if(action != null){
            if(action.equalsIgnoreCase(Actions.SHOW_FRIENDS_FRAGMENT)){
                showFriendsFragment();
            }
        }
    }catch(Exception e){

    }
}

问题是,如果在重新启动应用程序时在 onCreate 中找到该操作,则该操作仍然存在,因此它将始终在该页面上启动。 在 onNewIntent 中找到该操作时不会发生该行为,但这仅在应用程序运行时调用。

我尝试过使用额外内容并在显示片段后将其删除,将待处理的意图标志更改为CANCEL_CURRENTONE_SHOT,但行为仍然相同。

【问题讨论】:

    标签: android-intent google-cloud-messaging android-notifications android-fragmentactivity


    【解决方案1】:

    所以在尝试了几天之后。我一直找不到任何传统的方法来解决这个问题,所以我使用了 hack。

    1. 在您的主活动中定义一个public static boolean checkIntent = false
    2. 创建一个 Dummy Activity,当您点击通知时调用它

      final PendingIntent contentIntent = PendingIntent.getActivity(
          getApplicationContext(), requestID, new Intent(
                  getApplicationContext(), DummyActivity.class).putExtra("data", data), PendingIntent.FLAG_UPDATE_CURRENT);
      
    3. 在 DummyActivity 中

      @Override
      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      
      Intent i = getIntent();
      if (i != null) {
          String data = i.getStringExtra("data");
          Main.checkIntent = true;
          startActivity(new Intent(getBaseContext(), Main.class).putExtra(
                  "data", data));
      }
      finish();
      }
      
    4. 终于在checkIntent方法中

      private void checkIntent(Intent i){
      try{
      String data = i.getStringExtra("data");
      
      if(data!= null){
          checkIntent = false;
          showFriendsFragment();
      }
      }catch(Exception e){
      
      }
      }
      

    【讨论】:

    • 我认为应该是更好的方法。这不是解决问题的好方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多