我开发了一个使用解析推送通知服务的应用程序。 在整个过程中,我在 GenyMotion 上测试了该应用程序,同时将它与 android studio 一起使用。
解析推送在那里工作正常。
但是现在在开发之后,当我在运行 Android 4.1.2 的 Micromax Canvas A116 HD 中尝试相同的应用程序时,我根本没有收到任何推送通知。
我什至在广播接收器中设置了一个 BP,它一次都没有中断。
在 genyMotion 中运行的同一个应用程序如何获得通知。
以下是来自应用程序的代码:
Android list :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pchakraverti.enotice" >
<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" />
<permission android:protectionLevel="signature"
android:name="com.example.pchakraverti.enotice.permission.C2D_MESSAGE" />
<uses-permission android:name="com.example.pchakraverti.enotice.permission.C2D_MESSAGE" />
<application
android:name="com.example.pchakraverti.enotice.InitParse"
android:hardwareAccelerated="false"
android:allowBackup="true"
android:icon="@drawable/e_icon"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".clgIdVerificationActivity"
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=".StudentDetailsActivity"
android:label="@string/title_activity_student_details"
android:parentActivityName=".clgIdVerificationActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.pchakraverti.enotice.clgIdVerificationActivity" />
</activity>
<activity
android:name=".NotificationActivity"
android:label="@string/title_activity_notification"
android:parentActivityName=".StudentDetailsActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.pchakraverti.enotice.StudentDetailsActivity" />
</activity>
<activity
android:name=".WebViewActivity"
android:label="@string/title_activity_web_view" >
</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.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.example.pchakraverti.enotice" />
</intent-filter>
</receiver>
<receiver android:name="com.example.pchakraverti.enotice.MyBroadcastReceiver" 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>
初始化解析:
package com.example.pchakraverti.enotice;
import android.app.Application;
import android.util.Log;
import android.widget.Toast;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseInstallation;
import com.parse.SaveCallback;
/**
* Created by PChakraverti on 6/3/2015.
*/
public class InitParse extends Application {
@Override
public void onCreate(){
super.onCreate();
Parse.enableLocalDatastore(this);
Parse.initialize(this, "KEY", "KEY");
Toast.makeText(this, "Started", Toast.LENGTH_SHORT).show();
/*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);
}
}
});*/
ParseInstallation.getCurrentInstallation().saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
Log.d("com.parse.push", "Ready for push.");
} else {
e.printStackTrace();
}
}
});
}
}
我的广播接收者:
package com.example.pchakraverti.enotice;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
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;
import com.parse.ParsePushBroadcastReceiver;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Random;
import Database.Database;
import Database.NoticeInformation;
/**
* Created by PChakraverti on 6/3/2015.
*/
public class MyBroadcastReceiver extends ParsePushBroadcastReceiver {
@Override
public void onPushReceive(Context context, Intent intent) {
JSONObject json = null;
Log.i("TAG", "Push Received");
String data="", channel, action, f_name = "", n_id = "0", n_type = "", n_head = "", n_file = "";
try{
action = intent.getAction();
channel = intent.getExtras().getString("com.parse.Channel");
data = intent.getExtras().getString("com.parse.Data");
json = new JSONObject(data);
}
catch(Exception ex){
ex.printStackTrace();
}
try {
n_type = json.getString("N_TYPE");
n_head = json.getString("N_HEADER");
n_file = json.getString("N_FILE");
}catch (JSONException e){
e.printStackTrace();
}
//Save Information to Database
NoticeInformation ninfo = new NoticeInformation(0, n_type, n_head, +n_file, 0, 0);
Database db = new Database(context);
long r_id = db.insertIntoNoticeStorage(ninfo);
Intent launchIntent = new Intent(context, WebViewActivity.class);
launchIntent.putExtra("Link", n_file);
launchIntent.putExtra("N_ID", String.valueOf(r_id));
PendingIntent pi = PendingIntent.getActivity(context, new Random().nextInt(), launchIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.e_icon);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle("Push Notification")
.setContentText("A New Notice Was Published.")
.setContentIntent(pi)
.setAutoCancel(true)
.setLargeIcon(icon)
.setSmallIcon(R.drawable.e_icon)
.build();
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(1, notification);
}
}
我根本无法理解发生了什么以及如何解决这个问题。 这个跟安卓版本有关系吗?我没有任何配备最新 android 的设备,但在 Genymotion 上,该应用程序在 Android 4.1.1 中运行(收到推送)。
需要一些帮助!
最佳答案
我已经找到了问题的解决方案。 我已经在 android 4.1.2、4.3 中对此进行了测试。
worker 类(Class)给出如下:
package com.pchakraverti.enotice_new;
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.v7.app.NotificationCompat;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Random;
import database.*;
/**
* Created by PChakraverti on 6/6/2015.
*/
public class MyBroadcastReceiver extends BroadcastReceiver {
JSONObject json = null;
@Override
public void onReceive(Context context, Intent intent) {
Log.i("TAG", "Push Received");
String data="", channel, action, n_id = "0", n_type = "", n_head = "", n_file = "";;
try{
action = intent.getAction();
channel = intent.getExtras().getString("com.parse.Channel");
data = intent.getExtras().getString("com.parse.Data");
json = new JSONObject(data);
}
catch(Exception ex){
ex.printStackTrace();
}
try {
n_type = json.getString("N_TYPE");
n_head = json.getString("N_HEADER");
n_file = json.getString("N_FILE");
}catch (JSONException e){
e.printStackTrace();
}
NoticeInformation ninfo = new NoticeInformation(0, n_type, n_head, n_file, 0, 0);
Database db = new Database(context);
long r_id = db.insertIntoNoticeStorage(ninfo);
Intent launchIntent = new Intent(context, WebViewActivity.class);
launchIntent.putExtra("Link", n_file);
launchIntent.putExtra("N_ID", String.valueOf(r_id));
PendingIntent pi = PendingIntent.getActivity(context, new Random().nextInt(), launchIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.e_icon);
Notification notification = new NotificationCompat.Builder(context)
.setContentTitle("A notice has been published!")
.setContentText(n_head)
.setContentIntent(pi)
.setAutoCancel(true)
.setLargeIcon(icon)
.setSmallIcon(R.drawable.e_icon)
.build();
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.DEFAULT_VIBRATE;
notification.defaults |= Notification.DEFAULT_LIGHTS;
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(1, notification);
}
}
ParseBroacastReceiver 完全失败了。这怎么能完美地工作。
关于android - 解析推送通知在 Genymotion 中有效,但在真实手机中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30674482/