这 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 个原因..
activity 已经存在于 android backstack 中,extras 不在 onCreate() 中捕获,但它们可以在
onNewIntent()
中找到
如果附加项通过 PendingIntent Activity 传递,则按照官方文档。 http://developer.android.com/reference/android/app/PendingIntent.html .因此,要正确传递附加值,您需要确保每个 Intent 在action、data、type 方面都有区别、类 和类别。或者使用 FLAG_CANCEL_CURRENT 或 FLAG_UPDATE_CURRENT 取消系统中存在的当前 PendingIntent。
关于android - MainActivity 不会从 Firebase 通知 Intent 中获得额外的 yield ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47899011/