마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

파이어베이스 클라우드 메시징에서 url 을 받아오면 웹뷰를 띄우려고 합니다.

0 추천

안녕하세요.

현재 파이어베이스 클라우드 메시징에서 고급옵션> KEY&VALUE 에서 

key 에는 url, value 에는 웹페이지 주소를 주고 notification 을 받아서 클릭하면 mainActivity에서 loadUrl을 통해

웹페이지를 표시하려고 합니다.

그런데 암만 해봐도 잘 안되더군요.. 혹시 도와주시면 감사하겠습니다.

현재는 url을 보내도 기본으로 설정해둔 페이지만 뜨는 상태입니다.

 

------FirebaseMessagingService.class---------

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
    private static final String TAG = "FirebaseMsgService";

    // [START receive_message]
    @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());
        }
        Log.d(TAG, "onMessageReceived DATA: "+ remoteMessage.getData().get("message"));

        String url = remoteMessage.getData().get("url");
        sendPushNotification(remoteMessage.getData().get("message"), url);
    }

    private void sendPushNotification(String message, String url) {

        // String Title
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("url", url);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "default")
                .setSmallIcon(R.drawable.artkorea_iconimage)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.artkorea_iconimage))
                .setContentTitle("Push Title")
                .setColor(Color.RED)
                .setContentText(message)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setLights(000000255,500,2000)
                .setContentIntent(pendingIntent);


        // 알림 매니저
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // 오레오에서는 알림 채널을 매니저에 생성해야 한다
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            notificationManager.createNotificationChannel(new NotificationChannel("default", "기본 채널", NotificationManager.IMPORTANCE_DEFAULT));
        }

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
}

 

 

------MainActivity.class 에서---------

public class MainActivity extends AppCompatActivity {

    WebView mWebView;
    private static String originalURL = "https://www.google.com/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toast.makeText(MainActivity.this, "데이터를 불러오고 있습니다.",Toast.LENGTH_LONG).show();

        mWebView = (WebView) findViewById(R.id.webView_mainActivity);
     //  ----글자수때문에 중략했습니다---

        mWebView.loadUrl(originalURL);

        Intent intent = getIntent();
        if(intent.getStringExtra("url") != null) {
            String targetURL = intent.getStringExtra("url");
            Log.d("targetURL : ", targetURL);
            Toast.makeText(MainActivity.this, "targetURL" + targetURL, Toast.LENGTH_SHORT).show();
            mWebView.loadUrl(targetURL);
        }

        // FCM
        FirebaseMessaging.getInstance().subscribeToTopic("notice");
    }
}
toblerone 님이 2018년 8월 28일 질문
첨언하자면 포그라운드 상태에서는 정상동작하지만 백그라운드 상태에서 동작하지 않고있습니다!

1개의 답변

0 추천

 FCM 서비스 클래스는 이상이 없는것 같구, mWebView.loadUrl(originalUrl); 이 구문이 저위치에 있으믄 인탠트값으로 url이 넘어오든 넘어오지 않든 무조건 실행되는거 아닌가요??

익명사용자 님이 2018년 8월 29일 답변
...