1

我試圖通過Android中的Localytics推送通知實現深層鏈接。在下面的代碼中,我能夠接收通過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); 

} 
} 

與上面的代碼,我能打開OffersDisplayActivity上PushNotification接受,但我想,當我點擊推送通知要打開的OffersDisplayActivity。

請幫助我這個。謝謝!

回答

3

您不需要深度鏈接來滿足您的要求。 Localytics的人有時會誤以爲開發人員需要使用自定義類型的通知進行深度鏈接。

我們做了同樣的事情,你想在你的應用程序與localytics做。 1)在已經實施的GCMBroadcastReciever中接收Localytics信息。 2)你的消息保持一個字段,用於識別你想要的活動如果你添加任何額外的類用於接收意向與下面的操作

com.google.android.c2dm.intent.RECEIVE 
從GCMReceiver

除了將其刪除才能打開

..

通過這種方式,所有通知都來自您的服務器或localytics,它將在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或來自您自己的服務器,則發送包含字段"message_type"="news_notification"的GCM消息,然後用戶單擊通知將打開SingleNEwsActivity ,如果"message_type"=event_notification"那麼它將打開SingleEventActivity ..也可以在這裏傳遞額外的數據notificationIntent.putExtra()

+0

非常感謝..!自定義實施工作.. :) – Vina

+0

不客氣。 –

2

比較您的鍵值對並基於它,從Intent中調用慾望活動,同時生成推送通知。 它會在用戶點擊通知時調用它。

// Set the action to take when a user taps the notification 
    Intent resultIntent = new Intent(context, LoginActivity.class); 
    resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    if (notificationObj!=null) { 
     resultIntent.putExtra(UserDefault.pushJSONObj, notificationObj); 
    } 

    PendingIntent resultPendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT); 
    mBuilder.setContentIntent(resultPendingIntent); 

這裏notificationObj是你想傳遞給你的活動的參數。

+0

謝謝Bini。上述解決方案適用於我們創建自己的通知構建器的普通推送通知。但是由於我使用Localytics推送通知,因此我無法在此處設置'mBuilder'對象來設置PendingIntent。那就是我被卡住的地方.. – Vina