I first made an application for the video tutorial https://youtu.be/XM3THjZkRQM There about how to receive notifications from GCM and show notifications in the tray. The application is working. But my task is slightly different from the video of the lesson. From the server, a message is sent to GCM, from GCM it is received by the service, and then further in the service, instead of displaying push notifications in the tray, you send a specific command to activate the application (and the application will have many different activations, so you need activit, which is currently open). I decided to start small and try to make reception of the message in the MainActivity. There has already been implemented BroadCastReceiver, which was in the lesson. But he was set on accepting registration status in GCM.

Here is the main activation code:

public class MainActivity extends AppCompatActivity { private BroadcastReceiver mRegistrationBroadcastReciever; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mRegistrationBroadcastReciever = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Log.d("INTENT",intent.getAction()); if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_SUCCESS)){ String token = intent.getStringExtra("token"); Toast.makeText(getApplicationContext(),"GCM token: "+token, Toast.LENGTH_LONG).show(); } else if(intent.getAction().equals(GCMRegistrationIntentService.REGISTRATION_ERROR)){ Toast.makeText(getApplicationContext(),"GCM registration error!!!!", Toast.LENGTH_LONG).show(); } else { //tobe define } } }; int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); if(ConnectionResult.SUCCESS != resultCode){ if(GooglePlayServicesUtil.isUserRecoverableError(resultCode)){ Toast.makeText(getApplicationContext(),"Google Play Service is not installed/enable in this device", Toast.LENGTH_LONG).show(); GooglePlayServicesUtil.showErrorNotification(resultCode,getApplicationContext()); } else { Toast.makeText(getApplicationContext(),"This device do not support Google Play Service", Toast.LENGTH_LONG).show(); } } else{ Intent intent=new Intent(this,GCMRegistrationIntentService.class); startService(intent); } } @Override protected void onResume() { super.onResume(); Log.w("Main Activity","onResume"); LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReciever, new IntentFilter(GCMRegistrationIntentService.REGISTRATION_SUCCESS)); LocalBroadcastManager.getInstance(this).registerReceiver(mRegistrationBroadcastReciever, new IntentFilter(GCMRegistrationIntentService.REGISTRATION_ERROR)); } @Override protected void onPause() { super.onPause(); Log.w("Main Activity","onPause"); LocalBroadcastManager.getInstance(this).unregisterReceiver(mRegistrationBroadcastReciever); } } 

And here is the manifest file:

 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="test3.gcmtest"> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <permission android:name="test3.gcmtest.permission.C2D_MESSAGE" android:protectionLevel="signature" /> <uses-permission android:name="test3.gcmtest.permission.C2D_MESSAGE" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="com.google.android.gms.gcm.GcmReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND" > <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE"/> <category android:name="test3.gcmtest"/> </intent-filter> </receiver> <service android:name=".GCMPushReceiverService" android:exported="false"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE"/> </intent-filter> </service> <service android:name=".GCMRegistrationIntentService" android:exported="false"> <intent-filter> <action android:name="com.google.android.gms.iid.InstanceID"/> </intent-filter> </service> </application> </manifest> 

This is the service that is responsible for receiving messages from GCM and showing the push:

 public class GCMPushReceiverService extends GcmListenerService { @Override public void onMessageReceived(String from, Bundle data) { String message = data.getString("message"); String messageType = data.getString("messagetype"); Log.w("MESSAGE_TYPE",messageType); if(messageType.equals("push")){ Log.d("START","sender"); Intent pushIntent=new Intent("pusher"); LocalBroadcastManager.getInstance(this).sendBroadcast(pushIntent); } else { sendNotification(message); } } private void sendNotification(String message){ Intent intent = new Intent(this,MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); int requestCode = 0; PendingIntent pendingIntent=PendingIntent.getActivity(this, requestCode,intent,PendingIntent.FLAG_ONE_SHOT); Uri sound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.ic_launcher) .setContentText("My GCM message") .setContentText(message) .setAutoCancel(true) .setContentIntent(pendingIntent); NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(0,noBuilder.build()); } } 

In it, I need to process the received message and pull out the key, which will be the type of message. In one case, the push in the tray should work, and in the other case, you need to start a certain action in the main activation (then you need to change a certain value in the text field)

The first question is: do I send the intent correctly?

  Intent pushIntent=new Intent("pusher"); LocalBroadcastManager.getInstance(this).sendBroadcast(pushIntent); 

The second question: how to take this content in the MainActivity?

    1 answer 1

    While waiting for an answer, he figured out himself))

    I do not know why this did not work out before, but I did this (In any activity, not only in the main one):

      @Override protected void onResume() { super.onResume(); LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, new IntentFilter(GCMPushReceiverService.MESSAGE_SENDER)); } private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { //Действие, которое надо выполнить при получении сообщения: String message = intent.getStringExtra("message"); Log.d("receiver", "Got message: "+message); testTXT.setText(testTXT.getText()+"\n"+message); } }; @Override protected void onPause() { LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver); super.onPause(); } 

    Where GCMPushReceiverService.MESSAGE_SENDER is a static text constant that contains the name of the package and an arbitrary name. But I do not think that its contents should contain the name of the package.

    This source also helped to resolve the issue: http://www.vogella.com/tutorials/AndroidBroadcastReceiver/article.html