我已经尝试了这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);
} }
答案 0 :(得分:2)
如果您在意图中添加了额外内容并且未在接收活动中收到,则可能有两个原因。
活动已经存在于android backstack中,并且附加内容未在onCreate()中捕获,但可以在onNewIntent()
如果附加内容与PendingIntent的活动一起传递,那么根据官方文档。 http://developer.android.com/reference/android/app/PendingIntent.html。因此,要正确传递额外内容,您需要确保每个意图在操作,数据,类型方面存在差异,类和类别。或者,如果系统中存在使用 FLAG_CANCEL_CURRENT 或 FLAG_UPDATE_CURRENT ,则取消当前的PendingIntent。
答案 1 :(得分:0)
将标志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(),否则将为每个收到的通知重用相同的额外内容。
答案 2 :(得分:0)
扩展BroadcastReceiver
的{{1}}类。在onReceive()
BroadcastReceiver
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);
确保应用程序在已打开时不会重新打开。这意味着" old"首先打开FLAG_ACTIVITY_SINGLE_TOP
的意图被重新使用,它不会包含额外的值。您必须在YourActivity
中的另一个名为onNewIntent()
的方法中捕获它们。
YourActivity
请注意两个if语句 - 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
不知道它是否在onResume()
之后调用
答案 3 :(得分:0)
谢谢大家,最后我可以通过关注this post
让它发挥作用我只使用通知,禁用从我的php服务器发送的通知并使用数据只做了技巧,我可以发送通知,mainActivity确实从意图中捕获Putextra。
答案 4 :(得分:0)
如果在从最近状态或后台滑动应用程序时仍在寻找自定义数据(额外)firebase通知的人。
您可以在这里查看我的答案
https://github.com/firebase/quickstart-android/issues/96#issuecomment-449936698