【发布时间】:2018-05-02 12:54:47
【问题描述】:
我已经实现了 Firebase 推送通知,当应用程序处于前台时,我使用 Pending Intent 创建了一个自定义通知。单击通知时,它会打开一个 新活动。如果应用程序已打开并且活动正在运行,我不想重新创建它,而只是打开它。 我已将 launchMode 设置为“singleTask”,这可以解决问题。
问题是我通过 Intent 传递额外内容,而 onCreate 没有被调用并直接触发 onResume。
我怎样才能得到额外的东西。
我在这里做错什么了吗?您的帮助和建议将非常有帮助
这里是创建自定义通知待定意图的代码。
private void sendNotification(PushNotification pushNotification) {
try{
Intent intent = new Intent(this, BottomNavigationActivity.class);
intent.putExtra("pushNotification", (Serializable) pushNotification);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,(int) System.currentTimeMillis() /* request code */, intent,PendingIntent.FLAG_UPDATE_CURRENT);
long[] pattern = {500,500,500,500,500};
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(pushNotification.getTitle())
.setContentText(pushNotification.getMessage())
.setAutoCancel(true)
.setVibrate(pattern)
.setLights(Color.BLUE,1,1)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify( (int) System.currentTimeMillis() /* ID of notification */, notificationBuilder.build());
}catch (Exception e){
Log.d(TAG,e.getMessage());
}
}
清单
<activity
android:name=".activity.BottomNavigationActivity"
android:screenOrientation="portrait"
android:launchMode="singleTask">
</activity>
底部导航活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
Intent intent = getIntent();
PushNotification pushNotification = (PushNotification) intent.getSerializableExtra("pushNotification");
if(null != intent.getExtras()){
//here i get the pushnotification data and do something with it.
}
}catch (Exception e){
Log.d(TAG, e.getMessage());
}
}
@Override
protected void onResume() {
super.onResume();
}
谢谢 回复
【问题讨论】:
-
覆盖
onNewIntent()。由于启动模式为 singleTask,如果 Activity 处于前台,则 Intent 将传递给onNewIntent()。 -
我确实重写了这个函数,我应该在里面写什么。只是覆盖它没有任何区别,并且没有调用 onCreate
-
在此处处理接收到的意图(in
onNewIntent())。 -
好的,谢谢,现在试试。
-
这对我有用。谢谢你..
标签: android android-intent push-notification firebase-cloud-messaging android-pendingintent