android - MainActivity 不会从 Firebase 通知 Intent 中获得额外的 yield

标签 android firebase firebase-cloud-messaging

这 3 天我已经尝试处理 android 通知,用户点击通知然后 Activity 打开。但每次我 toast 时,它都说 null。 尝试使用 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);
} }

最佳答案

如果您已将额外内容添加到 Intent 中,但在接收 Activity 中未收到,可能有 2 个原因..

  1. activity 已经存在于 android backstack 中,extras 不在 onCreate() 中捕获,但它们可以在 onNewIntent()

  2. 中找到
  3. 如果附加项通过 PendingIntent Activity 传递,则按照官方文档。 http://developer.android.com/reference/android/app/PendingIntent.html .因此,要正确传递附加值,您需要确保每个 Intent 在actiondatatype 方面都有区别、类别。或者使用 FLAG_CANCEL_CURRENTFLAG_UPDATE_CURRENT 取消系统中存在的当前 PendingIntent。

关于android - MainActivity 不会从 Firebase 通知 Intent 中获得额外的 yield ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47899011/

相关文章:

javascript - 如何从谷歌表格中获取数据作为字典数组

java - 如何在 Android Studio 中使用 FCM 和 Bazel?

android - 如何重新安装 android 联系人应用程序以替换原来的应用程序进行调试?

jquery - Android 2.2.2 上的浏览​​器未使用 jQuery 获取 JSONP

firebase - 使用Firestore进行多查询和分页

java - Firebase:60 秒后删除数据

ios - Firebase 消息传递。 iOS 应用程序在后台模式或关闭时未收到通知

android - 我在向 Android 发送通知时收到 InvalidRegistration

android - 如何使用Android Intent/Market URI打开任何商店

android - 如何在 Python 中将 Google 帐户身份验证添加到 Google Cloud Endpoints