` <receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.Reading.home" />
</intent-filter>
</receiver>
<service android:name=".GcmMessageHandler" />
`
` public class API_gettoken extends AsyncTask {
Context context;
API_gettoken(Context context) {
this.context = context;
}
InterfaceSimpleStringResponce responce;
String regid;
void getresponce(InterfaceSimpleStringResponce responce) {
this.responce = responce;
}
@Override
protected Object doInBackground(Object... params) {
String PROJECT_NUMBER = "puthereprojectnumber";
GoogleCloudMessaging gcm;
gcm = GoogleCloudMessaging.getInstance(context);
try {
regid = gcm.register(PROJECT_NUMBER);
Log.v("testing...........", regid);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
responce.message(regid);
}
@Override
protected void onProgressUpdate(Object... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
@Override
protected void onCancelled(Object result) {
// TODO Auto-generated method stub
super.onCancelled(result);
}
@Override
protected void onCancelled() {
// TODO Auto-generated method stub
super.onCancelled();
}
}
`
`public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("---------------------------"
+ intent.getDataString());
// Explicitly specify that GcmMessageHandler will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmMessageHandler.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
public class GcmMessageHandler extends IntentService {
String mes, ChatID, FromUser, GroupID;
private Handler handler;
public GcmMessageHandler() {
super("GcmMessageHandler");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
handler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
mes = extras.getString("title");
ChatID = extras.getString("ChatID");
FromUser = extras.getString("FromUser");
GroupID = extras.getString("GroupID");
// showToast();
shownotification(mes);
Log.i("GCM",
"Received : (" + messageType + ") "
+ extras.getString("title"));
// GcmBroadcastReceiver.completeWakefulIntent(intent);
}
@SuppressLint("NewApi")
private void shownotification(String message) {
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
SharedPreferences sharedPrefs = getSharedPreferences(
Constants.PREFERENCE_NAME, Context.MODE_PRIVATE);
if (!T4JTwitterLoginActivity.isConnected(GcmMessageHandler.this)) {
Intent showIntent = new Intent(this, SignIn.class);
showIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
showIntent, 0);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
this).setContentTitle("Reading").setContentText(message)
.setDefaults(Notification.DEFAULT_ALL).setAutoCancel(true)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.mainicon);
Notification notification = mNotifyBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(0, notification);
} else {
ActivityManager activityManager = (ActivityManager) getBaseContext()
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> LIST = activityManager
.getRunningAppProcesses();
for (RunningAppProcessInfo runningAppProcessInfo : LIST) {
String PROSSES = runningAppProcessInfo.processName;
System.out.println(PROSSES);
}
if (sharedPrefs.contains("chatidx")) {
System.out.println((sharedPrefs.getString("chatidx", "")));
if (ChatID.equals(sharedPrefs.getString("chatidx", ""))) {
} else {
callmethood(message);
}
} else {
callmethood(message);
}
}
}
void callmethood(String message) {
NotificationManager mNotificationManager=
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent showIntent = new Intent(this, Send_message.class);
SharedPreferences sharedPrefs = getSharedPreferences(
Constants.PREFERENCE_NAME, Context.MODE_PRIVATE);
Editor e = sharedPrefs.edit();
e.putString("ADMINX", FromUser);
e.putString("chatidx", ChatID);
e.putString("GID", GroupID);
e.commit();
showIntent.putExtra("chatidx", ChatID);
showIntent.putExtra("ADMINX", FromUser);
showIntent.putExtra("GID", GroupID);
showIntent.putExtra("notification", ChatID);
showIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
showIntent, 0);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
this).setContentTitle("Reading").setContentText(message)
.setDefaults(Notification.DEFAULT_ALL).setAutoCancel(true)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.mainicon);
Notification notification = mNotifyBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(0, notification);
}
public void showToast() {
handler.post(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), mes, Toast.LENGTH_LONG)
.show();
}
});
}
}
`
`public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("---------------------------"
+ intent.getDataString());
// Explicitly specify that GcmMessageHandler will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmMessageHandler.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
public class GcmMessageHandler extends IntentService {
String mes, ChatID, FromUser, GroupID;
private Handler handler;
public GcmMessageHandler() {
super("GcmMessageHandler");
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
handler = new Handler();
}
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);
// The getMessageType() intent parameter must be the intent you received
// in your BroadcastReceiver.
String messageType = gcm.getMessageType(intent);
mes = extras.getString("title");
ChatID = extras.getString("ChatID");
FromUser = extras.getString("FromUser");
GroupID = extras.getString("GroupID");
// showToast();
shownotification(mes);
Log.i("GCM",
"Received : (" + messageType + ") "
+ extras.getString("title"));
// GcmBroadcastReceiver.completeWakefulIntent(intent);
}
@SuppressLint("NewApi")
private void shownotification(String message) {
NotificationManager mNotificationManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
SharedPreferences sharedPrefs = getSharedPreferences(
Constants.PREFERENCE_NAME, Context.MODE_PRIVATE);
if (!T4JTwitterLoginActivity.isConnected(GcmMessageHandler.this)) {
Intent showIntent = new Intent(this, SignIn.class);
showIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
showIntent, 0);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
this).setContentTitle("Reading").setContentText(message)
.setDefaults(Notification.DEFAULT_ALL).setAutoCancel(true)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.mainicon);
Notification notification = mNotifyBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(0, notification);
} else {
ActivityManager activityManager = (ActivityManager) getBaseContext()
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> LIST = activityManager
.getRunningAppProcesses();
for (RunningAppProcessInfo runningAppProcessInfo : LIST) {
String PROSSES = runningAppProcessInfo.processName;
System.out.println(PROSSES);
}
if (sharedPrefs.contains("chatidx")) {
System.out.println((sharedPrefs.getString("chatidx", "")));
if (ChatID.equals(sharedPrefs.getString("chatidx", ""))) {
} else {
callmethood(message);
}
} else {
callmethood(message);
}
}
}
void callmethood(String message) {
NotificationManager mNotificationManager=
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent showIntent = new Intent(this, Send_message.class);
SharedPreferences sharedPrefs = getSharedPreferences(
Constants.PREFERENCE_NAME, Context.MODE_PRIVATE);
Editor e = sharedPrefs.edit();
e.putString("ADMINX", FromUser);
e.putString("chatidx", ChatID);
e.putString("GID", GroupID);
e.commit();
showIntent.putExtra("chatidx", ChatID);
showIntent.putExtra("ADMINX", FromUser);
showIntent.putExtra("GID", GroupID);
showIntent.putExtra("notification", ChatID);
showIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
showIntent, 0);
NotificationCompat.Builder mNotifyBuilder = new NotificationCompat.Builder(
this).setContentTitle("Reading").setContentText(message)
.setDefaults(Notification.DEFAULT_ALL).setAutoCancel(true)
.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.mainicon);
Notification notification = mNotifyBuilder.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(0, notification);
}
public void showToast() {
handler.post(new Runnable() {
public void run() {
Toast.makeText(getApplicationContext(), mes, Toast.LENGTH_LONG)
.show();
}
});
}
}
`
-Service für das, was? Die Anmeldung im Manifest ist ausreichend. –
Nun, die 'OnReceive' wird nicht aufgerufen, wenn die App getötet wird – Alex
@Alex: Wie töten Sie die App? Kannst du die Schritte erwähnen? –