NextLeap blog

Push通知で目的のアプリページを開くには?

Deep Linkを使えば、push通知をクリックした時に、アプリトップ画面じゃない画面を開けるよ


ユーザのアクションを他のユーザにPush通知でお知らせしたい時ってありますよね。せっかく届いたPush通知をクリックしたらアプリのトップ画面って・・・少しがっかりさせちゃいますよねー。そんな時に利用したいのがこのDeepLinkです!

AndroidManufest.xml の編集

まずは、AndroidManufest.xml の編集をします。クリックした時に表示させたいActivityに下記の記述を追加

	
  <intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.BROWSABLE" />
    <data
      android:host="map"
      android:scheme="mobileapp" />
  </intent-filter>
				    

フラグメント使っているよという場合もあるでしょ。私もそうでした。試してみたけれどうまくいかなかったので、表示させたいフラグメントだけを呼び出すActivityを作ってあげることでうまくいきます

Push通知のコンテンツを編集

AWS SNS →FCM (Firebase Cloud Messaging)を使った例です。AWS SNSではアプリケーション名としてFCMでもGCMを指定します。GCMデータとして、"deepLink"のキーとバリューを設定します

"deepLink":"mobileapp://map"

この値はManufestに設定したschema://hostの形で記述します

	
    snsClient = boto3.client('sns')

    snsClient.publish(
        TopicArn="arn:aws:sns:ap-northeast-1:xxxxxxxx:{Topic名}",
        MessageStructure='json',
        Message=json.dumps({"default":"message", "GCM":json.dumps({"data":{"custom_title":"hogehoge","custom_body":"hogehoge", "deepLink":"mobileapp://map"}}) }),
        MessageAttributes={"pinType": {"DataType": "Number", "StringValue": pin_type}}
    )
				    

MessageAttributesはAWS SNSでフィルタリング機能を利用するためです。SNSについても今度書きますね

Push通知受信時のコードを編集

FCMを使っている例です。FirebaseMessagingService内で処理を行います

FirebaseMessagingServiceコードの要約
	
  public class MyFirebaseMessagingService extends FirebaseMessagingService {

      @Override
      public void onMessageReceived(RemoteMessage remoteMessage) {

        if (remoteMessage.getData().size() > 0) {
            Log.d(TAG, "Data Message");

            Map data = remoteMessage.getData();
            String customTitle = data.get("custom_title");
            Log.d(TAG, "Data Message Title :"+ customTitle);
            String customBody = data.get("custom_body");
            Log.d(TAG, "Data Message Body:" + customBody);
            String deepLink = data.get("deepLink");

            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(deepLink));

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

	        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
	        Notification.Builder notificationBuilder = new Notification.Builder(this)
	                .setSmallIcon(R.mipmap.ic_launcher)
	                .setContentTitle(customTitle)
	                .setContentText(customBody)
	                .setAutoCancel(true)
	                .setSound(defaultSoundUri)
	                .setContentIntent(pendingIntent);

	        NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
	        setChannel(manager, notificationBuilder);
	        manager.notify(0,notificationBuilder.build());

        }
      }
  }
				    

開いたActivityにデータを渡したい場合もそのデータをintentに格納して渡します

intent.putExtra("key", value);

Activity側でintentから読み取ります

	
  Integer val = getIntent().getIntExtra("key", defaultValue);