Android MainActivity在使用推送通知打开时将额外字符串获取为null
我正在使用webview应用程序,希望加载在通知中收到的url。 因此,我使用Android MainActivity在使用推送通知打开时将额外字符串获取为null,android,android-activity,push-notification,android-pendingintent,Android,Android Activity,Push Notification,Android Pendingintent,我正在使用webview应用程序,希望加载在通知中收到的url。 因此,我使用putExtra在主活动中接收url,但我得到的是null 这是我的代码FcmMessagingService代码- public class FcmMessagingService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) {
putExtra
在主活动中接收url,但我得到的是null
这是我的代码FcmMessagingService代码-
public class FcmMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.putExtra("fcm_url", "here url will be there");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,"Message");
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(message);
notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
notificationBuilder.setAutoCancel(true);
notificationBuilder.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("Message", "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build());
}
}
这是我在创建代码时的主要活动-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
final String fcm_message = intent.getStringExtra("fcm_url");
if(fcm_message!=null){
Log.d("Fcm_url",fcm_message);
}
else{
Log.d("Fcm_url","This is null");
}
...
当我直接打开应用程序时,logcat打印这是空的
,这很好,但当我发送通知并通过单击通知打开应用程序时,它会再次打印这是空的
,我做错了什么
更新
当我将Log.d..
添加到我的onMessageRecieved
中时,不会执行它,请参见
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("on_message_recieved","fcm message recieved");
....
但收到通知后不打印
我在舱单上加了这个-
<service
android:name=".FcmMessagingService"
android:permission="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
似乎它没有执行我的自定义
FcmMessagingService
,但为什么???从FCM获取值到main活动请尝试下面的代码
if (getIntent().getExtras() != null) {
String fcm_url= getIntent().getExtras().getString("fcm_url")
}
删除此链接并重试
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
我已经试过了,在你的回答之后我又试了一次,但一直以来
getIntent().getExtras()
都是null
,还有其他建议吗?在这个问题上只发布一个答案。抱歉,但没有效果检查我的答案它可能会解决你的问题@Inasecat感谢您的回复,但已经尝试了解决方案,我认为问题不同,请查看我的更新问题您可以注册FCMidService请检查更新的答案。现在是否称为FcmMessagingService?我正在尝试从服务器发送数据消息而不是通知,如果问题解决了,我会通知您