gpt4 book ai didi

android - 应用程序在后台时如何处理推送 firebase 通知单击事件?

转载 作者:行者123 更新时间:2023-11-30 00:39:56 27 4
gpt4 key购买 nike

我是 Android 新手。

当应用程序打开并且通知来自 firebase 并单击通知时,它会打开通知 Activity ,但是当应用程序处于前台/后台并且收到通知并单击通知时,它会自动打开 mainActivity。

我正在将数据从 FirebaseMessagingService.class 广播到主 Activity 。

为什么?

我的代码如下。

主 Activity

   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>

最佳答案

您应该使用 WakefulBroadcastReceiver 的实现而不是 FirebaseMessagingService

   public class NotificationsReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
//process data from intent
abortBroadcast();
}
}

在你的 list 中

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

关于android - 应用程序在后台时如何处理推送 firebase 通知单击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42694839/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com