【问题标题】:MainActivity doesn't get extra from Firebase Notification intentMainActivity 没有从 Firebase 通知意图中获得额外信息
【发布时间】:2017-12-20 04:53:59
【问题描述】:

我已经尝试了这 3 天来处理 android 通知,用户点击通知然后 Activity 打开。但是每次我设置吐司时,它都会显示为空。 尝试使用 SOF 的一些解决方案,但不起作用。你能看看代码有什么问题吗?提前致谢。

通知代码是

private void sendPushNotification(JSONObject json) {
    //optionally we can display the json into log
    int notificationId = new Random().nextInt();

    Log.e(TAG, "Notification JSON " + json.toString());
    try {
        //getting the json data
        JSONObject data = json.getJSONObject("data");

        //parsing json data
        String title = data.getString("title");
        String message = data.getString("message");
        String imageUrl = data.getString("image");

        // Instantiate a Builder object.
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this.getApplicationContext(),"Default");

        Intent notifyIntent = new Intent(this, MainActivity.class);
        notifyIntent.putExtra("fromNotification", true);
        // Sets the Activity to start in a new, empty task
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        // Creates the PendingIntent
        PendingIntent pendingIntent =
                PendingIntent.getActivity(
                        this.getApplicationContext(), notificationId,
                        notifyIntent,
                        PendingIntent.FLAG_ONE_SHOT
                );

        //int id = 1;
        // Puts the PendingIntent into the notification builder
        builder.setContentIntent(pendingIntent);
        // Notifications are issued by sending them to the
        // NotificationManager system service.
        NotificationManager mNotificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // Builds an anonymous Notification object from the builder, and
        // passes it to the NotificationManager
        if (mNotificationManager != null) {
            mNotificationManager.notify(notificationId, builder.build());
        }


    } catch (JSONException e) {
        Log.e(TAG, "Json Exception: " + e.getMessage());
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    }
}

而mainActivity.class是

public class MainActivity extends AppCompatActivity {



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setElevation(0);
    }

    // Open Tab
    if(getIntent().getExtras() != null){

        Bundle b = getIntent().getExtras();
        boolean cameFromNotification = b.getBoolean("fromNotification");

        Toast.makeText(this.getApplicationContext(),
                "Error: " + getIntent().getExtras(),
                Toast.LENGTH_LONG).show();

        viewPager = findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = findViewById(R.id.tabs);
        viewPager.setCurrentItem(1);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();

    }
    else {
        viewPager = findViewById(R.id.viewpager);
        setupViewPager(viewPager);

        tabLayout = findViewById(R.id.tabs);
        viewPager.setCurrentItem(0);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();

    }



}



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

【问题讨论】:

标签: android firebase firebase-cloud-messaging


【解决方案1】:

如果您在意图中添加了额外内容,但在接收活动中未收到,可能有两个原因..

  1. Activity 已经存在于 android backstack 中,额外的没有在 onCreate() 中被捕获,但它们可以在onNewIntent()中找到

  2. 如果附加功能是通过 PendingIntent 活动传递的,则按照官方文档。 http://developer.android.com/reference/android/app/PendingIntent.html。因此,要正确传递附加信息,您需要确保每个意图在 actiondatatype 方面都有区别、类别。或者,如果系统中存在当前的 PendingIntent,则使用 FLAG_CANCEL_CURRENTFLAG_UPDATE_CURRENT

【讨论】:

    【解决方案2】:

    将标志 PendingIntent.FLAG_ONE_SHOT 更改为 PendingIntent.FLAG_UPDATE_CURRENT

    PendingIntent pendingIntent =
                PendingIntent.getActivity(
                        this.getApplicationContext(), notificationId,
                        notifyIntent,
                        PendingIntent. FLAG_UPDATE_CURRENT
                );
    

    如果我们使用 Intent Extras,我们必须调用带有 PendingIntent.FLAG_UPDATE_CURRENT 标志的 PendingIntent.getActivity(),否则相同的 extras 将被重复用于收到的每个通知。

    【讨论】:

    • 已经尝试过 FLAG_UPDATE_CURRENT,如 SOF 中所述,但仍然无法正常工作
    【解决方案3】:

    在扩展BroadcastReceiverBroadcastReceiver 类中。在onReceive()中放入如下代码

    Intent intent2open = new Intent(context, YourActivity.class);
    intent2open.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent2open.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    String name = "KEY";
    String value = "String you want to pass";
    intent2open.putExtra(name, value);
    context.startActivity(intent2open);
    

    FLAG_ACTIVITY_SINGLE_TOP 确保应用在已打开的情况下不会重新打开。这意味着首先打开 YourActivity 的“旧”意图会被重新使用,它不会包含额外的值。您必须在 YourActivity 中使用另一个名为 onNewIntent() 的方法来捕获它们。

    public class YourActivity extends Activity {
        private String memberFieldString;
    
        @Override
        public void onCreate(Bundle savedInstanceState) { 
             super.onCreate(savedInstanceState);
             setContentView(R.layout.main);
    
             // Code doing your thing...
        } // End of onCreate()
    
        @Override
        protected void onNewIntent(Intent intent) {
        Log.d("YourActivity", "onNewIntent is called!");
    
        memberFieldString = intent.getStringExtra("KEY");
    
        super.onNewIntent(intent);
    } // End of onNewIntent(Intent intent)
    
        @Override
        protected void onResume() {
            if (memberFieldString != null) {
                if (opstartsIntent.getStringExtra(KEY) != null) {
                   Log.d("YourActivity", "memberFieldString: "+ memberFieldString);
                } else {
                   Log.d("YourActivity", "The intent that started YourActivity did not have an extra string value");
                }
            }
        } // End of onResume()
    
    }  // End of YourActivity
    

    请注意两个 if 语句 - onResume() 不知道它是否在 OnCreate()->OnStart() OR onRestart()->onStart() 之后调用

    【讨论】:

    【解决方案4】:

    谢谢大家,我终于可以通过关注this post使其工作了

    我只使用通知,在禁用从我的 php 服务器发送的通知并仅使用数据后,我可以发送通知,而 mainActivity 确实从意图中捕获了 Putextra。

    【讨论】:

      【解决方案5】:

      如果有人在从最近或后台滑动应用时仍在寻找获取自定义数据(Extras)firebase 通知。

      你可以看看我的回答

      https://github.com/firebase/quickstart-android/issues/96#issuecomment-449936698

      【讨论】:

        猜你喜欢
        • 2018-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多