2

Android で Localytics プッシュ通知を介してディープ リンクを実装しようとしています。以下のコードでは、プッシュ通知の作成中に Localytics ダッシュボードを介して送信するキーと値のペアを受け取ることができます。ただし、私の要件は、プッシュ通知で受け取ったキーと値のペアに基づいて特定のアクティビティを開くことです。

     public class GCMReceiver extends BroadcastReceiver {
     String deeplink_key = "KEY_DEEPLINK";
     public static final String CUSTOM_INTENT ="com.mypackage.action.TEST";

     @Override
     public void onReceive(Context context, Intent intent) {
    Bundle extras = intent.getExtras();
    String deeplinkValues = extras.getString(deeplink_key);
    Log.i("BASE", "deeplinkValues: " + deeplinkValues);
    String action = intent.getAction();
    Uri data = intent.getData();

    Intent gotoOffersIntent = new Intent(context,OffersDisplayActivity.class);
    gotoOffersIntent.putExtra(deeplink_key, deeplinkValues);
//  gotoOffersIntent.setAction(CUSTOM_INTENT);
    /*The below line opens the OffersDisplayActvity directly when Push notification is received*/
    context.startActivity(gotoOffersIntent);


//  context.sendOrderedBroadcast(gotoOffersIntent, null);

    PushReceiver pushReceiver = new PushReceiver();
    pushReceiver.onReceive(context, intent);

    GCMBroadcastReceiver gcmBroadcastReceiver = new GCMBroadcastReceiver();
    gcmBroadcastReceiver.onReceive(context, intent);

}
}

上記のコードを使用すると、受信した PushNotification で OffersDisplayActivity を開くことができますが、プッシュ通知をクリックしたときに OffersDisplayActivity を開く必要があります。

これで私を助けてください.ありがとう!

4

2 に答える 2

3

要件にディープ リンクは必要ありません。Localytics の担当者は、カスタム タイプの通知にはディープリンクが必要だと言って、開発者に誤解を与えることがあります。

アプリでやりたいことと同じことを localytics で行いました。1)すでに実装されている GCMBroadcastReciever で Localytics 情報を受信します。2)あなたのメッセージでは、開きたいアクティビティを識別するためのフィールドを1つ保持します

次のアクションでインテントを受け取るための余分なクラスを追加した場合

com.google.android.c2dm.intent.RECEIVE

GCMReceiver とは別に、それを削除します。

そのようにして、すべての通知はサーバーまたはローカルから送信され、onReceive メソッドで受信されます。

localytics と独自のサーバーに対して行った完全な例を次に示します。

Android Manifest.xml

<service
            android:name=".gcm.CustomInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>

        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <!-- for Gingerbread GSF backward compat -->
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.nearfox.android" />
            </intent-filter>
        </receiver>

        <service android:name=".gcm.CustomGCMListenerService">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service
            android:name=".gcm.RegistrationIntentService"
            android:exported="false" />

CustomGCMListenerService.java 内

public class CustomGCMListenerService extends GcmListenerService {

    private static final String TAG = "CustomGCMListener";

    public interface MESSAGE_TYPE {
        String NOTIFICATION_NEWS = "news_notification";
        String NOTIFICATION_EVENT = "event_notification";
    }

    @Override
    public void onMessageReceived(String from, Bundle data) {
        if (data.containsKey("msg_type") && data.getString("msg_type") != null) {
            String messageType = data.getString("msg_type");
            if (messageType.equals(MESSAGE_TYPE.NOTIFICATION_NEWS)) {
                String newsJson = data.getString("news_body");
                try {
                    JSONObject jsonObject = new JSONObject(newsJson).getJSONObject("message");
                    generateNotification(this, jsonObject.getString("title"), "", MESSAGE_TYPE.NOTIFICATION_NEWS, data);
                } catch (JSONException e) {
                    e.printStackTrace();
                    Log.i(TAG, "Notification Parsing Error");
                    return;
                }
            } else if (messageType.equals(MESSAGE_TYPE.NOTIFICATION_EVENT)) {
                String newsJson = data.getString("body");
                try {
                    JSONObject jsonObject = new JSONObject(newsJson).getJSONObject("message");
                    generateNotification(this, jsonObject.getString("title"), "", MESSAGE_TYPE.NOTIFICATION_EVENT, data);
                } catch (JSONException e) {
                    e.printStackTrace();
                    Log.i(TAG, "Notification Parsing Error");
                    return;
                }
            }
        }
    }


    public static void generateNotification(Context context, String message, String ids, String messageType, Bundle data) {
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
        notificationBuilder.setSmallIcon(R.drawable.small_notification_icon);
        notificationBuilder.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.app_icon));
        String title = context.getString(R.string.app_name);
        notificationBuilder.setContentTitle(title);
        notificationBuilder.setContentText(message);
        Notification notification ;


        if (messageType.equals(MESSAGE_TYPE.NOTIFICATION_NEWS)) {
            Intent notificationIntent = new Intent(context, SingleNewsActivity.class);
            notificationIntent.putExtra("source", "notification");
            notificationIntent.putExtra("news_title", message);
            PendingIntent intent =
                    PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            notificationBuilder.setContentIntent(intent);
        } else if (messageType.equals(MESSAGE_TYPE.NOTIFICATION_EVENT)) {
            Intent notificationIntent = new Intent(context, SingleEventActivity.class);
            notificationIntent.putExtra("source", "notification");
            notificationIntent.putExtra("event_title", data);
            PendingIntent intent =
                    PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            notificationBuilder.setContentIntent(intent);
        }
        notificationBuilder.setContentText(message);
        notificationBuilder.setStyle(new android.support.v4.app.NotificationCompat.BigTextStyle().bigText(message));
        notification = notificationBuilder.build();
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notificationManager.notify(0, notification);

    }
}

したがって、ここでは、localytics または独自のサーバーからフィールドを含む GCM メッセージを送信したかどうかを確認できます。"message_type"="news_notification"ユーザーが通知をクリックすると、SingleNEwsActivity"message_type"=event_notification"が開き、SingleEventActivity が開きます。また、ここで追加のデータを渡すこともできますnotificationIntent.putExtra()

于 2015-11-09T08:47:53.873 に答える