2 回答

TA貢獻1806條經(jīng)驗 獲得超5個贊
有一些方法可以實現(xiàn)這一目標。但是stackoverflow上有很多類似的問題。
該方法取決于您的 fcm 通知負載。請看這個答案。你會找到你正在尋找的所有東西。
https://stackoverflow.com/a/40185654/7140884

TA貢獻1859條經(jīng)驗 獲得超6個贊
一開始你不應(yīng)該同時創(chuàng)建通知和發(fā)送廣播消息。嘗試這個:
public class MyFirebaseInstanceService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID = "com.example.theroos.simplifiedmsging02.test";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification",NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("SIMPLIFIED");
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.setVibrationPattern(new long[]{0,100,500,1000});
notificationChannel.enableLights(true);
//notificationChannel.createNotificationChannel(notificationChannel);
}
Intent activityintent = new Intent(this,HomeActivity.class);
PendingIntent contentintent = PendingIntent.getActivity(this,0,activityintent,0);
NotificationCompat.Builder notificationbuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
notificationbuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setSmallIcon(R.drawable.ic_mode_comment_black_24dp)
.setContentTitle(title)
.setContentIntent(contentintent)
.setContentText(body)
.setColor(Color.BLUE)
.setAutoCancel(true)
.setContentInfo("Info");
notificationManager.notify(new Random().nextInt(),notificationbuilder.build());
}
....
然后PendingIntent.getActivity你可以創(chuàng)建PendingIntent.getBroadcast(YourBroadcastReceiver..)你應(yīng)該在清單中注冊的廣播接收器:
<receiver
android:name="com.app.YourBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="YourReceiverIntentFilter" />
</intent-filter>
</receiver>
在YourBroadcastReceiver您應(yīng)該獲得待處理的意圖,然后在您的活動中向您的本地接收器發(fā)送廣播消息。
希望能幫助到你!
添加回答
舉報