【发布时间】:2016-10-04 16:52:46
【问题描述】:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webview);
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if(extras != null){
if (extras.containsKey("URL")) {
// extract the extra-data in the Notification
String url = extras.getString("URL");
mWebView.loadUrl(url);
} else {
mWebView.loadUrl("http://example.com/");
}
} else {
mWebView.loadUrl("http://example.com/");
}
// Stop local links and redirects from opening in browser instead of WebView
mWebView.setWebViewClient(new MyAppWebViewClient());
}
@Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
}
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
//It is optional
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getBody());
}
//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Log.d("BODY", messageBody);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Firebase Push Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
我的应用程序正在使用 WebView,并希望有一些 Notification 函数将用户引导到 HTTP 页面,使用 Firebase 云消息传递通知。
我正在使用 Firebase 控制台并使用自定义数据发送通知(参见图 1)。
使用上面的代码,如果应用程序没有打开或在后台,当用户点击通知时,我成功地将用户引导到页面,但是如果应用程序在前台打开,但它不起作用。
我相信我应该添加 getExtras() 和 extra.getString("URL") 的东西而不是 onCreate 方法,但我不知道在哪里添加,像 onResume 或什么?
我不是 Android 开发人员,但我目前的任务是完成将网站嵌入到带有通知的应用程序中。谢谢!
【问题讨论】:
-
您从哪里发送通知? Firebase 控制台还是您自己的后端?
-
@SudipPodder firebase 控制台
标签: android firebase push-notification android-notifications firebase-cloud-messaging