我正在尝试为我的WebView Android应用程序设置Firebase推送通知。我的目标是,我将在通知中发送一个URL,并将在应用程序中打开。这是我的问题。
以下是发送消息的PHP代码
<?php
// API access key from Google API's Console
define( 'API_ACCESS_KEY', 'XXXXX' );
$id='XXXXXX';
$registrationIds = array($id);
// prep the bundle
$msg = array
(
'body' => 'Your post has received a reply',
'title' => 'Title of the post here',
);
$data = array
(
'url'=>'http://XXXXXXXX.com/forum/viewtopic.php?f=2&p=21421#p21421'
);
$fields = array
(
'registration_ids' => $registrationIds,
'notification' => $msg,
'data' => $data
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://android.googleapis.com/gcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
这是我的清单
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MyFirebaseMessagingService$NotificationBar" />
<service android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service android:name=".FirebaseIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
这是我的接收器
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
public static String url;
public static final String TAG = "FCM Service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Data: " + remoteMessage.getData());
url = remoteMessage.getData().get("url");
Log.d(TAG, "URL: " + url);
}
public class NotificationBar extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_bar);
}
}
}
从这里开始,我将URL传递到主要活动。现在的问题是,如果该应用在前景中,并且我发送了一条消息,我可以在日志中看到URL。但是,当该应用在后台并发送消息时,我只会看到状态中的通知,而日志中没有任何信息。如果我点击通知,则只需在不传递URL的情况下打开应用程序即可。
您能帮忙吗?请尽可能细节,因为我对Android编程的想法几乎为零。
谢谢!
请参阅此处的说明:
onMessageReceived
用于大多数消息类型,并带有 以下例外:当您的应用在后台中,发送通知消息。在 在这种情况下,通知已交付到设备的系统托盘上。 用户点击通知将默认打开应用启动器。
带有通知和数据有效载荷的消息,背景和 前景。在这种情况下,通知已发送到 设备的系统托盘,数据有效载荷已在Extras中交付 启动器活动的意图。
您应该在启动器活动意图的附加功能中检查数据有效载荷,这意味着如果HomeActivity
是您在清单中的启动器活动,则应在HomeActivity's
onCreate
方法中检查:
Intent intent = getIntent();
String url = intent.getStringExtra("url");
...