如何获得推送并发送推送。 我想接收推送消息并在活动上显示消息, 我尝试过以下操作,但找不到ParsePushBrodcastReciver。
我能够发送它并收到它,但我无法将信息保存在某个变量中
package com.parse.starter;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
public class ParsePushReciever extends ParsePushBroadcastReceiver {
@Override
public void onPushOpen(Context context, Intent intent) {
AppLog.e("Push", "Clicked");
Intent i = new Intent(context, MainActivity.class);
i.putExtras(intent.getExtras());
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Push Notification",intent.getExtras().get(ParsePushBroadcastReceiver.KEY_PUSH_DATA).toString());
NotificationManager nm=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
builder.setSmallIcon(R.drawable.launcher);
Intent newIntent=new Intent(context,MainActivity.class);
newIntent.putExtra(context.getString(R.string.navigation_from_notification),true);
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pi=PendingIntent.getActivity(context, 0, newIntent, 0);
builder.setContentIntent(pi);
builder.setContentText("Push Notification");
Log.d("Notification", strMsg);
nm.notify(1, builder.build());
}
}
答案 0 :(得分:0)
public static void generateNotification(Context context, String title, String message, String action) {
// Show the notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher).setContentTitle(title).setContentText(message)
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher));
Intent intent = new Intent(context, SplashScreen.class);
intent.putExtra(KEY_TITLE, title);
intent.putExtra(KEY_BODY, message);
intent.setAction(action);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
mBuilder.setSound(soundUri);
// Creates an explicit intent for an Activity in your app
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(SplashScreen.class);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
// used for random integer generation
mBuilder.setAutoCancel(true);
Random random = new Random();
mNotificationManager.notify(random.nextInt(), mBuilder.build());
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
if (v != null)
v.vibrate(1000);
}
尝试使用此代码生成通知,并在意图中打开包含所需消息的通知。
答案 1 :(得分:0)
这个小代码可以通过推送获得所有文本,你可以用它来做你的事情:
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
JSONObject json = null;
try {
json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
String text = json.getString("alert").toString();
} catch (JSONException e) {
e.printStackTrace();
}
}
将它放在onReceive()方法中。
我希望这对你有所帮助。
答案 2 :(得分:0)
我找到了有用的例子
public class MyCustomReceiver extends BroadcastReceiver {
private static final String TAG = "MyCustomReceiver";
public static final String intentAction = "SEND_PUSH";
@Override
public void onReceive(Context context, Intent intent) {
if (intent == null) {
Log.d(TAG, "Receiver intent null");
} else {
// Parse push message and handle accordingly
processPush(context, intent);
}
}
private void processPush(Context context, Intent intent) {
String action = intent.getAction();
Log.d(TAG, "got action " + action );
if (action.equals(intentAction))
{
String channel = intent.getExtras().getString("com.parse.Channel");
try {
JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));
Log.d(TAG, "got action " + action + " on channel " + channel + " with:");
// Iterate the parse keys if needed
Iterator<String> itr = json.keys();
while (itr.hasNext()) {
String key = (String) itr.next();
// Extract custom push data
if (key.equals("customdata")) {
// Handle push notification by invoking activity directly
launchSomeActivity(context, json.getString(key));
// OR trigger a broadcast to activity
triggerBroadcastToActivity(context);
// OR create a local notification
createNotification(context);
}
Log.d(TAG, "..." + key + " => " + json.getString(key));
}
} catch (JSONException ex) {
Log.d(TAG, "JSON failed!");
}
}
}
public static final int NOTIFICATION_ID = 45;
// Create a local dashboard notification to tell user about the event
private void createNotification(Context context) {
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context).setSmallIcon(
R.drawable.ic_launcher).setContentTitle("Successfully logged in");
NotificationManager mNotificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(45, mBuilder.build());
}
// Handle push notification by invoking activity directly
private void launchSomeActivity(Context context, String datavalue) {
Intent pupInt = new Intent(context, ShowPopUp.class);
pupInt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
pupInt.putExtra("customdata", datavalue);
context.getApplicationContext().startActivity(pupInt);
}
// Handle push notification by sending a local broadcast
// to which the activity subscribes to
private void triggerBroadcastToActivity(Context context) {
LocalBroadcastManager.getInstance(context).sendBroadcast(new Intent(intentAction));
}
}
为此...这将有助于你
https://github.com/codepath/ParsePushNotificationExample
并且不要忘记将广播Receiver添加到Manifest
<receiver
android:name=".MyReceiver"
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>