Android 在通知时运行浏览器并单击
我需要从Firebase云消息接收包含标题和url的消息,显示带有这些标题和url的通知,然后单击此通知,在浏览器中打开此url。 我写了这段代码:Android 在通知时运行浏览器并单击,android,push-notification,android-notifications,firebase-cloud-messaging,Android,Push Notification,Android Notifications,Firebase Cloud Messaging,我需要从Firebase云消息接收包含标题和url的消息,显示带有这些标题和url的通知,然后单击此通知,在浏览器中打开此url。 我写了这段代码: import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; impor
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.util.Log;
import android.widget.Toast;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0) {
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null) {
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle());
}
}
private void sendNotification(String url, String MessageTitle) {
Toast.makeText(this, url, Toast.LENGTH_SHORT).show();
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
//Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
//.setSmallIcon(R.drawable.ic_stat_ic_notification)
.setContentTitle(MessageTitle)
.setContentText(url)
.setAutoCancel(true)
//.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
但点击通知后,应用程序会打开,但不会打开浏览器。我的错误是什么?
谢谢从代码上看,您似乎只是在“sendNotification”中显示通知,但您不再处理该通知了?好的,那么单击后如何处理这些通知?