我需要从Firebase Cloud Message接收包含标题和网址的消息,显示带有这些标题和网址的通知,点击此通知后,在浏览器中打开此网址。 我写了这段代码:
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());
}
}
但点击通知应用程序后打开,但不是浏览器。我的错误是什么? 感谢