解析通知适用于模拟器,但不适用于真实设备

时间:2015-04-25 10:50:11

标签: android parse-platform

我正在研究Parse notification..to向所有用户发送通知...以下代码在模拟器上运行良好,但真实设备无法获取通知.... 继承我的代码: MainActivity.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ParseAnalytics.trackAppOpenedInBackground(getIntent());
    PushService.setDefaultPushCallback(this, MainActivity.class);
    ParseInstallation.getCurrentInstallation().saveInBackground();
     }
     }

ParseApplication.java

public class ParseApplication extends Application{

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    Parse.initialize(this, "[APP ID]", "[CLIENT KEY]");
    ParseUser.enableAutomaticUser();
    ParseACL defaultACL = new ParseACL();
    defaultACL.setPublicReadAccess(true);
    ParseACL.setDefaultACL(defaultACL, true);



    ParseInstallation.getCurrentInstallation().saveInBackground();
   }

Receiver.java

public class Receiver extends ParsePushBroadcastReceiver{

@Override
protected void onPushOpen(Context arg0, Intent arg1) {
    // TODO Auto-generated method stub
    Intent i = new Intent(arg0,MainActivity.class);
    i.putExtras(arg1.getExtras());
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    arg0.startActivity(i);

}

的AndroidManifest.xml

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="11" />
 <uses-permission android:name="android.permission.INTERNET" />
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
  <uses-permission android:name="android.permission.VIBRATE" />
  <uses-permission android:name="android.permission.WAKE_LOCK"/>
  <uses-permission android:name="android.permission.GET_ACCOUNTS" />
  <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
  <permission android:protectionLevel="signature"
  android:name="com.example.parse.permission.C2D_MESSAGE" />
  <uses-permission android:name="com.example.parse.permission.C2D_MESSAGE" />
  <application
    android:allowBackup="true"
    android:name="com.example.parse.ParseApplication" 
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"
    >
    <activity
        android:name="com.example.parse.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <service android:name="com.parse.PushService" />
    <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>
    <receiver 
        android:name="com.example.parse.Receiver"
        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>


  </application>

 </manifest>

1 个答案:

答案 0 :(得分:0)

您没有订阅设置。如果您还没有看到它,请点击链接。 Parse Android Push Tutorial

在您的申请中:创建:

ParsePush.subscribeInBackground("", new SaveCallback() {
  @Override
  public void done(ParseException e) {
    if (e == null) {
      Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
    } else {
      Log.e("com.parse.push", "failed to subscribe for push", e);
    }
  }
});

由于您可能正在创建自己的示例应用程序,请密切关注Manifest配置,确保您拥有正确的软件包名称。 正如Parse文档中所述,当Play-Services可用时,他们会使用GCM,这是实际的Android设备所具有的。他们在没有播放服务的设备上使用持久连接的地方,即。你的模拟器。希望这可以帮助。