0

您好,我正在使用以下教程爲android.I開發的應用程序解析推送通知。 http://www.androidhive.info/2015/06/android-push-notifications-using-parse-com/。我可以在一個棒棒糖設備上得到通知。我沒有在kitkat和其他設備上得到通知。我沒有找到問題。Android分析推送通知在某些設備中不起作用

這裏是我的清單文件

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.android_new_user.testnotification" > 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-permission android:name="android.permission.WAKE_LOCK" /> 
    <uses-permission android:name="android.permission.VIBRATE" /> 
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
    <uses-permission android:name="android.permission.GET_ACCOUNTS" /> 
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" /> 

    <permission 
     android:name="com.example.android_new_user.testnotification.permission.C2D_MESSAGE" 
     android:protectionLevel="signature" /> 

    <uses-permission android:name="com.example.android_new_user.testnotification.permission.C2D_MESSAGE" /> 

    <application 
     android:name=".MyApplication" 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.android_new_user.testnotification.LoginActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <activity 
      android:name="com.example.android_new_user.testnotification.MainActivity" 
      android:label="@string/app_name" /> 

     <service android:name="com.parse.PushService" /> 

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

</manifest> 

和我reciver文件是看起來像

package com.example.android_new_user.testnotification; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 

import com.parse.ParsePushBroadcastReceiver; 

import org.json.JSONException; 
import org.json.JSONObject; 
/** 
* Created by Android_new_user on 11/25/2015. 
*/ 
public class CustomPushReciver extends ParsePushBroadcastReceiver { 
    private final String TAG = CustomPushReciver.class.getSimpleName(); 

    private NotificationUtils notificationUtils; 

    private Intent parseIntent; 

    public CustomPushReciver() { 
     super(); 
    } 

    @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()); 
     } 
    } 

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

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

    /** 
    * Parses the push notification json 
    * 
    * @param context 
    * @param json 
    */ 
    private void parsePushJson(Context context, JSONObject json) { 
     try { 
      boolean isBackground = json.getBoolean("is_background"); 
      JSONObject data = json.getJSONObject("data"); 
      String title = data.getString("title"); 
      String message = data.getString("message"); 

      if (!isBackground) { 
       Intent resultIntent = new Intent(context, MainActivity.class); 
       showNotificationMessage(context, title, message, 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.putExtras(parseIntent.getExtras()); 

     intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 

     notificationUtils.showNotificationMessage(title, message, intent); 
    } 
} 

和我的應用程序類

public class MyApplication extends Application { 

    private static MyApplication mInstance; 

    @Override 
    public void onCreate() { 
     super.onCreate(); 
     mInstance = this; 

     // register with parse 
     ParseUtils.registerParse(this); 
    } 


    public static synchronized MyApplication getInstance() { 
     return mInstance; 
    } 
} 

我怎樣才能解決這個問題。請幫助在此感謝

+0

下面的代碼。你能解決這個問題? –

回答

0

您是否已將應用程序名稱添加到清單文件中?

而且還覈對清單

<permission 
    android:name="yourpackagename.permission.C2D_MESSAGE" 
    android:protectionLevel="signature" /> 

<uses-permission android:name="yourpackagename.permission.C2D_MESSAGE" /> 

<receiver 
     android:name="com.parse.ParsePushBroadcastReceiver" 
     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" /> 
     </intent-filter> 
    </receiver> 
    <receiver 
     android:name="com.parse.GcmBroadcastReceiver" 
     android:permission="com.google.android.c2dm.permission.SEND" > 
     <intent-filter> 
      <action android:name="com.google.android.c2dm.intent.RECEIVE" /> 
      <action android:name="com.google.android.c2dm.intent.REGISTRATION" /> 
      <category android:name="yourpackagename" /> 
     </intent-filter> 
    </receiver>