简体   繁体   English

在Android上解析接收通知

[英]Parse receiving notifications on Android

In my android app I'm getting this error when I try to receive push notification in logcat: 在我的Android应用中,当我尝试在logcat中接收推送通知时出现此错误:

6-22 14:38:16.016 6570-6570/com.tekinarslan.material.sample E/com.parse.ManifestInfo﹕ Cannot use GCM for push because the app manifest is missing some required declarations. 6-22 14:38:16.016 6570-6570 / com.tekinarslan.material.sample E / com.parse.ManifestInfo:无法使用GCM进行推送,因为应用清单缺少某些必需的声明。 Please make sure that these permissions are declared as children of the root element: Also, please make sure that these services and broadcast receivers are declared as children of the element: 06-22 14:38:16.021 6570-6570/com.tekinarslan.material.sample E/com.parse.PushService﹕ Tried to use push, but this app is not configured for push due to: Push is not configured for this app because the app manifest is missing required declarations. 请确保将这些许可权声明为root元素的子代:此外,请确保将以下服务和广播接收者声明为root元素的子代:06-22 14:38:16.021 6570-6570 / com.tekinarslan。 material.sample E / com.parse.PushService:尝试使用推,但由于以下原因,该应用未配置为推:由于未显示必需的声明,因此未为此应用配置推。 Please add the following declarations to your app manifest to support either GCM or PPNS for push (or both). 请在您的应用清单中添加以下声明,以支持GCM或PPNS进行推送(或同时支持两者)。 To enable GCM support, please make sure that these permissions are declared as children of the root element: Also, please make sure that these services and broadcast receivers are declared as children of the element: To enable PPNS support, please make sure that these permissions are declared as children of the root element: Also, please make sure that these services and broadcast receivers are declared as children of the element: 要启用GCM支持,请确保将这些权限声明为root元素的子元素:此外,请确保将这些服务和广播接收者声明为该元素的子元素:要启用PPNS支持,请确保这些权限被声明为根元素的子元素:此外,请确保将这些服务和广播接收者声明为元素的子元素:

I think that I have all I need in my manifest, here it is: 我认为清单中有我所需要的,它是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tekinarslan.material.sample"
    android:versionCode="1"
    android:versionName="1.0">
    <uses-sdk android:minSdkVersion="9"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <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.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <!-- Permissions required for parse.com notifications -->
    <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"/>
    <!-- END Parse permissions -->

    <!--
      IMPORTANT: Change "com.parse.tutorials.pushnotifications.permission.C2D_MESSAGE" in the lines below
      to match your app's package name + ".permission.C2D_MESSAGE".
    -->
    <permission android:protectionLevel="signature"
        android:name="com.tekinarslan.material.sample.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.tekinarslan.material.sample.permission.C2D_MESSAGE" />

    <application
        android:name=".App"
        android:allowBackup="true"
        android:label="DONUT"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/facebook_app_id"/>
        <activity
            android:name=".SampleActivity"
            android:label="@string/app_name" >

        </activity>
        <activity
            android:name=".FbLoginActivity" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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


        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar"/>

        <!-- My custom receiver -->
        <receiver android:name=".ParseReceiver" >
            <intent-filter>
                <action android:name="com.tekinarslan.material.sample.RECEIVE_PUSH" />
            </intent-filter>
        </receiver>
        <!-- END my custom receiver -->


        <!-- Required for Parse.com notifications -->
        <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>
        <!-- END Parse.com requirements -->

    </application>

</manifest>

**MY ParseReceiver - ** **我的ParseReceiver-**

package com.tekinarslan.material.sample;

import java.util.Iterator;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
/*****************************
 * This class will receive custom push notifications
 * from parse.com. These are different than the "plain"
 * message push notifications. 
 * 
 * There must be an action defined within the Intent-Filter
 * for this receiver in the manifest.xml file. And the same
 * action must be specified on the notification when it is
 * pushed.
 * 
 * You can optionally pass JSON data from parse.com which will
 * be avaialable in the onReceive() method here.
 *****************************/
public class ParseReceiver extends BroadcastReceiver {
    private final String TAG = "Parse Notification";
    private String msg = "";
    @Override
    public void onReceive(Context ctx, Intent intent) {
        Log.i(TAG, "PUSH RECEIVED!!!");

        try {
            String action = intent.getAction();
            String channel = intent.getExtras().getString("com.parse.Channel");
            JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

            Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
            Iterator itr = json.keys();
            while (itr.hasNext()) {
                String key = (String) itr.next();
                Log.d(TAG, "..." + key + " => " + json.getString(key));
                if(key.equals("string")){
                    msg = json.getString(key);
                }
            }
        } catch (JSONException e) {
            Log.d(TAG, "JSONException: " + e.getMessage());
        }


        Bitmap icon = BitmapFactory.decodeResource(ctx.getResources(),
                R.drawable.ic_launcher);

        Intent launchActivity = new Intent(ctx, SampleActivity.class);
        PendingIntent pi = PendingIntent.getActivity(ctx, 0, launchActivity, 0);

        Notification noti = new NotificationCompat.Builder(ctx)
        .setContentTitle("PUSH RECEIVED")
        .setContentText(msg)
        .setSmallIcon(R.drawable.ic_launcher)
        .setLargeIcon(icon)
        .setContentIntent(pi)
        .setAutoCancel(true)
        .build();

        NotificationManager nm = (NotificationManager)ctx.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(0, noti);

    }

}

your custom receiver needs the following actions in the intent-filters: 您的自定义接收方需要在intent过滤器中执行以下操作:

<receiver
  android:name=".ParseReceiver"
  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="at.a1telekom.android.newsroom"/>
  </intent-filter>
</receiver>

EDIT: 编辑:

are you subscribing to parse like this? 您正在订阅这样解析吗?

Parse.initialize(context, appId, clientKey);    
ParsePush.subscribeInBackground( yourChannels, new SaveCallback() {...});

You are missing given code in the manifest. 您在清单中缺少给定的代码。 Include given code at the bottom of the Manifest file before tag. 在Manifest文件底部的标记之前添加给定的代码。

 <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" />

            <!-- IMPORTANT: Change "com.parse.tutorials.pushnotifications" to match your app's package name. -->
            <category android:name="com.tekinarslan.material.sample" />
        </intent-filter>
    </receiver>
    <receiver
        android:name="com.tekinarslan.material.sample.NotificationReceiver"
        android:exported="false" >
        <intent-filter>
            <action android:name="com.tekinarslan.material.sample.UPDATE_STATUS" />
        </intent-filter>
    </receiver>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM