2016-04-27 209 views
0

实现使用该tutorial自定义通知

问题自定义通知现在越来越2周的通知。 一个是默认通知和其他是定制通知。如何禁用默认通知。

代码:

public void showNotificationMessages(String title, String message, Intent intent) { 
     int icon = R.mipmap.ic_launcher; 
     PendingIntent resultPendingIntent = 
       PendingIntent.getActivity(
         mContext, 
         0, 
         intent, 
         PendingIntent.FLAG_UPDATE_CURRENT 
       ); 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mContext) 
       .setSmallIcon(icon) 
       .setContentTitle("Project") 
       .setContentText(""+title+" custom notification") 
       .setContentIntent(resultPendingIntent) 
       .setAutoCancel(true); 
     int mNotificationId = 001; 
     NotificationManager mNotifyMgr = 
       (NotificationManager) mContext. getSystemService(Context.NOTIFICATION_SERVICE); 
     mNotifyMgr.notify(mNotificationId, mBuilder.build()); 
} 

舱单

<receiver android:name=".receiver.CustomPushReceiver" 
     android:exported="false"> 
     <intent-filter> 
      <action android:name="com.parse.push.intent.RECEIVE" /> 
      <action android:name="com.parse.push.intent.DELETE" /> 
      <action android:name="com.parse.push.intent.OPEN" /> 
      <action android:name="android.intent.action.BOOT_COMPLETED" /> 
      <action android:name="android.intent.action.USER_PRESENT" /> 
     </intent-filter> 
    </receiver> 

截图

Screenshot

CustomReceiver

@Override 
protected void onPushReceive(Context context, Intent intent) { 
    super.onPushReceive(context, intent); 

    if (intent == null) 
     return; 

    try { 
     JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data")); 

     Log.e(TAG, "Push received: " + json); 

     parseIntent = intent; 

     parsePushJson(context, json); 

    } catch (JSONException e) { 
     Log.e(TAG, "Push message json exception: " + e.getMessage()); 
    } 
} 
private void parsePushJson(Context context, JSONObject json) { 
    try { 

     String title = json.getString("alert"); 

     Intent resultIntent = new Intent(context, MainActivity.class); 
     showNotificationMessage(context, title, "", resultIntent); 


    } catch (JSONException e) { 
     Log.e(TAG, "Push message json exception: " + e.getMessage()); 
    } 
} 
    private void showNotificationMessage(Context context, String title, String message, Intent intent) { 

    notificationUtils = new NotificationUtils(context); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    notificationUtils.showNotificationMessages(title, message, intent); 
} 
+0

你可以分享你的'.receiver.CustomPushReceiver'吗? –

+0

现在分享此方法代码'parsePushJson' ..共享完整的代码在哪里构建通知。 – Bharatesh

+0

请检查我编辑的答案@JPn请 –

回答

1

可能您正试图操纵您的分析通知。

所以,如果你改变你的onPushReceive

@Override 
protected void onPushReceive(Context context, Intent intent) { 
    try { 
    JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data")); 

    Log.e(TAG, "Push received: " + json); 

    parseIntent = intent; 

    parsePushJson(context, json); 

} catch (JSONException e) { 
    Log.e(TAG, "Push message json exception: " + e.getMessage()); 
} 
    return; 
} 

您将无法通过删除super.OnPushReceive()摆脱解析您的推送通知,所以你会看到只有您的自定义推送通知

0

这将是巨大的如果你分享你的CustomReceiver,但是从我的经验来看,你应该扩展GcmReceiver或其他任何可能已经扩展它的类。