推送通知 - 在单击通知、应用运行时获取意图信息



我已经实现了 Firebase 推送通知,当应用处于前台时,我会使用待处理的意图创建自定义通知。单击通知时,它会打开一个 新活动。如果应用程序已打开并且活动正在运行,我不想重新创建它,而只是打开它。 我已将启动模式设置为"单任务",这可以解决问题。

这样做的问题是我正在传递带有意图的额外内容,并且 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();
}

谢谢 R

为此,您必须使用广播接收器。

当您在 Firebase 服务中收到来自 Firebase 的推送时,请将本地广播发送到您所需的活动。对于该检查,第一个应用是否正在运行。

例:

在您的 FCM 消息服务中放置此方法以检查应用程序是否正在运行

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;
}

in onReceive Method into 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.
}
};

最新更新