我使用Parse.com发送推送通知。但在这个应用程序中,当我发送推送时,没有任何内容出现。我该怎么做才能解决这个问题?
P.S.-我正在制作具有不同名称的相同类型的应用程序。在之前的应用中,会显示通知,但不会出现此通知。
这是我在java类中添加的Push通知类:
public class PushNotificationReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
try {
System.out.println("Amber====>" + intent.getExtras() + "====" + intent.getAction());
if (intent.getAction().equals(context.getPackageName() + ".UPDATE_STATUS")) {
JSONObject json = null;
try {
String jStr = intent.getExtras().getString("com.parse.Data");
json = new JSONObject(jStr);
} catch (Exception e) {
json = new JSONObject();
e.printStackTrace();
}
String title = "New alert!";
if (json.has("adstuck_val")) {
SimpleDateFormat formatter = new SimpleDateFormat("MMM dd yy hh:mm aaa", Locale.getDefault());
String date = formatter.format(new Date());
json.put("date", date);
System.out.println("" + json.optInt("case"));
title = json.getString("customdata");
if (VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
showBoardingPassNotif(context, json.optInt("case"), json.optString("adstuck_val"),
title, json.optString("link"), json.optInt("id"));
} else {
generateNotification(context, json.optInt("case"), json.optString("adstuck_val"),
title, json.optString("link"), json.optInt("id"));
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void showBoardingPassNotif(final Context context, final int notifType,
final String desc, final String title, String link, final int id) {
final NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
final Notification notif;
if (VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notif = new Notification.Builder(context)
.setContentTitle(desc)
.setContentText(title)
.setSmallIcon(R.drawable.ic_launcher_material)
.setStyle(new Notification.BigTextStyle().bigText(title))
.setVibrate(new long[]{500,500})
.build();
notif.color = context.getResources().getColor(R.color.orange);
} else {
notif = new Notification.Builder(context)
.setContentTitle(desc)
.setContentText(title)
.setSmallIcon(R.drawable.ic_launcher)
.setStyle(new Notification.BigTextStyle().bigText(title))
.setVibrate(new long[]{500, 500})
.build();
}
Intent notificationIntent = null;
PendingIntent contentIntent = null;
switch (notifType) {
case 1: {
//System.out.println("Amber in case 0");
notificationIntent = new Intent(context, Splash.class);
notificationIntent.putExtra("caseI", 1);
contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notif.contentIntent = contentIntent;
break;
}
case 2: {
//System.out.println("Amber in case 0");
notificationIntent = new Intent(context, Splash.class);
notificationIntent.putExtra("caseI", 2);
contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notif.contentIntent = contentIntent;
break;
}
case 3: {
//System.out.println("Amber in case 0");
notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notif.contentIntent = contentIntent;
break;
}
}
ParseAnalytics.trackAppOpenedInBackground(notificationIntent);//rackAppOpened(getIntent());
notif.flags = Notification.FLAG_AUTO_CANCEL;
nm.notify(id, notif);
}
//5feb7a1c-2d8b-45ae-83a4-9037c9a47bb1
public static void generateNotification(Context context, int notifType,
String desc, String titleStr, String link, final int id) {
// Show the notification
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String title = titleStr; //context.getString(R.string.app_name);
Intent notificationIntent = null;
PendingIntent contentIntent = null;
switch (notifType) {
case 1: {
notificationIntent = new Intent(context, Splash.class);
contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notificationIntent.putExtra("caseI", 1);
contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
break;
}
case 2: {
notificationIntent = new Intent(context, Splash.class);
notificationIntent.putExtra("caseI", 2);
contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
break;
}
case 3: {
notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(link));
contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
break;
}
}
Notification notification/* = new Notification(R.drawable.ic_launcher, message, when)*/;
ParseAnalytics.trackAppOpenedInBackground(notificationIntent);//rackAppOpened(getIntent());
NotificationCompat.Builder builder = new NotificationCompat.Builder(
context);
notification = builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher).setTicker(desc).setWhen(when)
.setAutoCancel(true).setContentTitle(title)
.setContentText(desc).build();
notification.vibrate = new long[]{500, 500};
notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notification.flags =
Notification.FLAG_AUTO_CANCEL |
Notification.FLAG_SHOW_LIGHTS;
notificationManager.notify(id, notification);
}
}
以下是我在AndroidManifest.cml文件中添加的内容 -
<!--Parse-->
<service android:name="com.parse.PushService" />
<receiver android:name="com.raccoonfinger.fruits.PushNotificationReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.RECEIVE_BOOT_COMPLETED" />
<action android:name="android.intent.action.USER_PRESENT" />
<action android:name="com.raccoonfinger.fruitss.UPDATE_STATUS" />
</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>
<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="com.raccoonfinger.fruits" />
</intent-filter>
</receiver>
<meta-data
android:name="com.parse.APPLICATION_ID"
android:value="@string/parse_app_id" />
<meta-data
android:name="com.parse.CLIENT_KEY"
android:value="@string/parse_client_key" />
<meta-data
android:name="com.crashlytics.ApiKey"
android:value="fce48f279ed5f83910d1cca39605c0ea877ec296" />
</application>