我开发了一款使用解析推送通知服务的应用。 通过整个过程,我在GenyMotion上测试了应用程序,同时在android studio中使用它。
解析推送工作正常。
但是现在开发之后,当我在运行Android 4.1.2的Micromax Canvas A116 HD中尝试使用相同的应用程序时,我只是没有得到任何推送通知。
我甚至在广播接收器中设置了BP,它甚至没有打破过它。
genyMotion中运行的同一个应用程序如何获取通知。
以下是应用中的代码:
Android Manifest:
<?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>
InitParse:
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();
}
}
});
}
}
MyBroadcastReceiver:
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版本有关吗?我没有任何具有最新android的设备,但在Genymotion上,该应用程序在Android 4.1.1中工作(推送已收到)。
需要一些帮助!
答案 0 :(得分:1)
我找到了问题的解决方案。 我在android 4.1.2,4.3中测试了这个。
工人阶级如下:
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完全失败。这怎么可能完美。
答案 1 :(得分:0)
你为什么评论ParsePush.subscribeInBackground()
?
如果您删除此评论,此代码应该有效。
我有一个与初始化和订阅完全相同的应用程序。(确保解析时应用程序设置上的“启用客户端启用”选项为“是”)。