当应用程序在后台时如何处理push firebase通知点击事件? [英] how to handle push firebase notification click event when the application is in background?
问题描述
当应用程序打开并且firebase发出通知并单击通知时,它会打开Notification Activity,但是当应用程序在前台/后台和通知来到,然后单击通知,然后它自动打开mainActivity。
我将FirebaseMessagingService.class中的数据广播到主活动。
为什么?
我的代码在下面。 strong> MainActivity
public class MainActivity extends AppCompatActivity {
$ b $ @Override
保护无效的onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocalBroadcastManager.getInstance(this)
.registerReceiver(mHandler,new IntentFilter(Pakage_name_FCM-MESSAGE));
private BroadcastReceiver mHandler = new BroadcastReceiver(){
@Override
public void onReceive(Context context,Intent intent){
String title = intent.getStringExtra( 标题);
String message = intent.getStringExtra(message);
Log.d(MainActivity-title,title);
Log.d(MainActivity-message,message);
}
};
@Override
protected void onPause(){
super.onPause();
//取消注册广播接收器
LocalBroadcastManager.getInstance(this).unregisterReceiver(mHandler);
FirebaseNotificationIdService.Class / p>
public class FirebaseNotificationIdService extends FirebaseInstanceIdService {
private static final String REG_TOKEN =REG_TOKEN;
@Override //每次创建一个新的系统调用这个方法
public void onTokenRefresh(){
String recent_token = FirebaseInstanceId.getInstance()。getToken();
Log.d(REG_TOKEN,recent_token);
$ / code $ / pre
$ b FirebaseMessagingService.class
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
$ b $ @Override
public void onMessageReceived(RemoteMessage remoteMessage){
Log.d(Remote Form,remoteMessage.getFrom());
//检查消息是否包含数据...
if(remoteMessage.getData()。size()> 0){
Log.d(Message Data ,Message Data ---+ remoteMessage.getData());
String title = remoteMessage.getData()。get(title);
String message = remoteMessage.getData()。get(message);
Intent intent = new Intent(package_name_FCM-MESSAGE);
intent.putExtra(title,title);
intent.putExtra(message,message);
Log.d(INTENT-TITLE,title);
Log.d(INTENT-MESSAGE,message);
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
localBroadcastManager.sendBroadcast(intent);
检查消息是否包含通知
$ b $ if(remoteMessage.getNotification()!= null){
Log.d( Message Body,---+ remoteMessage.getNotification()。getBody());
sendNotification(remoteMessage.getNotification()。getBody());
$ b $ //显示通知
private void sendNotification(String body){
意图意图=新意图(this,NotificationActivity.class );
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0 / * Request code * /,intent,PendingIntent.FLAG_ONE_SHOT);
//设置声音
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this);
notifiBuilder.setSmallIcon(R.mipmap.ic_launcher);
notifiBuilder.setContentTitle(Aadhaar Update);
notifiBuilder.setContentText(body);
notifiBuilder.setAutoCancel(true);
notifiBuilder.setSound(notificationSound);
notifiBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 / *通知的ID * /,notifiBuilder.build());
Minifest.xml $ b $ / p>
< activity android:name =。MainActivity>
< intent-filter>
< category android:name =android.intent.category.LAUNCHER/>
< / intent-filter>
< / activity>
< service android:name =。FirebaseNotificationIdService>
< intent-filter>
< action android:name =com.google.firebase.INSTANCE_ID_EVENT/>
< / intent-filter>
< / service>
< service android:name =。FirebaseMessagingService>
< intent-filter>
< action android:name =com.google.firebase.MESSAGING_EVENT/>
< / intent-filter>
< / service>
< activity android:name =。NotificationActivity>< / activity>
解决方案您应该使用WakefulBroadcastReceiver而不是FirebaseMessagingService的实现 p>
public class NotificationsReceiver extends WakefulBroadcastReceiver {
$ b $ @Override
public void onReceive(Context context,Intent意图){
//从意图处理数据
abortBroadcast();
$在你的清单
< receiver
android:name =。fcm.NotificationsReceiver
android:exported =true
android:权限= com.google.android.c2dm.permission.SEND >
< intent-filter android:priority =999>
< action android:name =com.google.android.c2dm.intent.RECEIVE/>
< / intent-filter>
< / receiver>
I'm New in Android.
When the application is open and notification came from firebase and click the notification then it open the Notification Activity but when the application is in foreground/background and notification came and click the notification then it automatically open the mainActivity.
I'm broadcasting the data from FirebaseMessagingService.class to Main Activity.
Why?
My code is below.
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocalBroadcastManager.getInstance(this)
.registerReceiver(mHandler,new IntentFilter("Pakage_name_FCM-MESSAGE"));
}
private BroadcastReceiver mHandler = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String title = intent.getStringExtra("title");
String message = intent.getStringExtra("message");
Log.d("MainActivity-title",title);
Log.d("MainActivity-message",message);
}
};
@Override
protected void onPause() {
super.onPause();
// Unregister the Broadcast receiver
LocalBroadcastManager.getInstance(this).unregisterReceiver(mHandler);
}
}
FirebaseNotificationIdService.Class
public class FirebaseNotificationIdService extends FirebaseInstanceIdService {
private static final String REG_TOKEN = "REG_TOKEN";
@Override // each time a new created and system call this method
public void onTokenRefresh() {
String recent_token= FirebaseInstanceId.getInstance().getToken();
Log.d(REG_TOKEN,recent_token);
}
}
FirebaseMessagingService.class
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("Remote Form",remoteMessage.getFrom());
// check if the message contains data...
if (remoteMessage.getData().size()> 0){
Log.d("Message Data", "Message Data---"+remoteMessage.getData() );
String title=remoteMessage.getData().get("title");
String message=remoteMessage.getData().get("message");
Intent intent = new Intent("package_name_FCM-MESSAGE");
intent.putExtra("title",title);
intent.putExtra("message",message);
Log.d("INTENT-TITLE",title);
Log.d("INTENT-MESSAGE",message);
LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(this);
localBroadcastManager.sendBroadcast(intent);
}
//check if the message contain notification
if (remoteMessage.getNotification()!=null){
Log.d("Message Body","---"+remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody());
}
}
//Display the notification
private void sendNotification(String body) {
Intent intent = new Intent(this,NotificationActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0/*Request code*/,intent,PendingIntent.FLAG_ONE_SHOT);
//Set sound
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notifiBuilder = new NotificationCompat.Builder(this);
notifiBuilder.setSmallIcon(R.mipmap.ic_launcher);
notifiBuilder.setContentTitle("Aadhaar Update");
notifiBuilder.setContentText(body);
notifiBuilder.setAutoCancel(true);
notifiBuilder.setSound(notificationSound);
notifiBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0/*Id of Notification*/,notifiBuilder.build());
}
}
Minifest.xml
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".FirebaseNotificationIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<activity android:name=".NotificationActivity"></activity>
解决方案 You should use implementation of WakefulBroadcastReceiver instead of FirebaseMessagingService
public class NotificationsReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//process data from intent
abortBroadcast();
}
}
In your manifest
<receiver
android:name=".fcm.NotificationsReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter android:priority="999">
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
</intent-filter>
</receiver>
这篇关于当应用程序在后台时如何处理push firebase通知点击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文