1

MainActivity.javaFCM通知,通知時,請單擊應用程序在前臺後臺不

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()); 
    } 
} 

enter image description here

我的應用程序正在使用的WebView和希望有一些Notification的功能將用戶引導至HTTP頁面,並使用Firebase雲消息傳遞進行通知。

我正在使用Firebase控制檯並使用自定義數據發送通知(請參閱圖像1)。

使用上面的代碼,當用戶點擊通知(如果應用程序未打開或在後臺)時,我成功地將用戶引導至頁面,但如果應用程序在前臺打開但不起作用。

我相信我應該加上getExtras()extra.getString("URL")之外的東西,除了onCreate的方法,但是我不知道要在哪裏添加,比如onResume還是什麼?

我不是一名Android開發人員,但我目前有一項任務,即通過通知完成在應用程序內嵌入網站。謝謝!

+0

從哪裏發送通知? Firebase控制檯或您自己的後端? –

+0

@SudipPodder Firebase控制檯 – user259752

回答

0

我找到了解決方案,在MyFirebaseMessagingService.java添加幾行代碼,並在前臺和後臺

不知道應用程序工作,這是這樣做的

MyFirebaseMessagingService.java的正確方法

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 com.google.firebase.messaging.FirebaseMessagingService; 
import com.google.firebase.messaging.RemoteMessage; 

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     String link = null; 

     if (remoteMessage.getData().containsKey("URL")) { 
      link = remoteMessage.getData().get("URL"); 
     } 

     //Calling method to generate notification 
     sendNotification(remoteMessage.getNotification().getBody(), link); 
    } 

    //This method is only generating push notification 
    //It is same as we did in earlier posts 
    private void sendNotification(String messageBody, String link) { 
     Intent intent = new Intent(this, MainActivity.class); 
     intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
     if (link != null) { 
      intent.putExtra("URL", link); 
     } 

     PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
       PendingIntent.FLAG_ONE_SHOT); 

     Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this); 
      notificationBuilder.setSmallIcon(R.mipmap.ic_launcher); 
      notificationBuilder.setContentTitle("THE APP NAME"); 
      notificationBuilder.setContentText(messageBody); 
      notificationBuilder.setAutoCancel(true); 
      notificationBuilder.setSound(defaultSoundUri); 
      notificationBuilder.setContentIntent(pendingIntent); 

     NotificationManager notificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     notificationManager.notify(0, notificationBuilder.build()); 
    } 
} 
相關問題