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