我已经实施了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>
BottomNavigationActivity
@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();
}
由于 [R
答案 0 :(得分:0)
为此,您必须使用广播接收器。
当您从firebase服务中的firebase接收推送时,请将本地广播发送到您所需的活动。对于该检查,第一个应用程序是否正在运行。
示例:
在您的FCM MessagingService中,使用此方法检查该应用是否正在运行
private boolean isAppIsInBackground(Context context) {
boolean isInBackground = true;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
if (am != null && am.getRunningAppProcesses() !=null) {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT_WATCH) {
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String activeProcess : processInfo.pkgList) {
if (activeProcess.equals(context.getPackageName())) {
isInBackground = false;
}
}
}
}
} else {
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
if (componentInfo.getPackageName().equals(context.getPackageName())) {
isInBackground = false;
}
}
}
return isInBackground;
}
将onReceive方法导入FirebaseMessagingService
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (isAppIsInBackground(getApplicationContext())) {
// send broadcast
Bundle extras = intent.getExtras();
Intent i = new Intent("broadCastName");
// Data you need to pass to activity
i.putExtra("message","messageData");
context.sendBroadcast(i);
} else {
// send extra with intent and start activity
Intent intent=new Intent(getApplicationContext(),YourActivity.class);
intent.putExtra("message","messageData");
startActivity(intent)
/* or here you can also broadcast with data and one flag bit
then into your activity when you received this flag bit start that activity again from itself.
*/
}
}
在活动类
中添加此内容 BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle b = intent.getExtras();
String message = b.getString("message");
Log.e("newmesage", "" + message);
// here you will get your extra data. Use that as required and handle.
}
};