我正在尝试使用FCM开发推送通知。我能够在我的设备上获得通知并点击通知,应用程序打开MainActivity页面(activity_main.xml)但它应该直接打开SecondActivity.java(second.xml)并且它还必须在Textview中显示通知消息。 这是我的代码 -
MainActivity.java
public class MainActivity extends Activity {
private static final String TAG="MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button BtnshowToken=(Button)findViewById(R.id.buttonshowtoken);
BtnshowToken.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String token= FirebaseInstanceId.getInstance().getToken();
Log.d(TAG,"Token: "+token);
Toast.makeText(MainActivity.this,token,Toast.LENGTH_SHORT).show();
}
});
}
}
MyFirebaseInstanceIDService.java
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG="MyFirebaseInsIDService";
@Override
public void onTokenRefresh() {
//get updated token
String refreshedToken= FirebaseInstanceId.getInstance().getToken();
Log.d(TAG,"New Token: "+refreshedToken);
}
}
MyFirebaseMessagingService.java
public class MyFirebaseMessagingService extends FirebaseMessagingService{
private static final String TAG="MyFirebaseMsgService";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "FROM:" +remoteMessage.getFrom());
//Check if msg contains data
if(remoteMessage.getData().size() > 0){
Log.d(TAG, "Message data: "+remoteMessage.getData());
}
//Check if msg contains notification
if(remoteMessage.getNotification()!=null){
Log.d(TAG,"Message body: "+remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody());
}
}
//Display notification
private void sendNotification(String body){
Intent intent=new Intent(this,SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent=PendingIntent.getActivity(this, 0/*Request code*/, intent, PendingIntent.FLAG_ONE_SHOT);
//Set sound of notification
Uri notificationsound= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notifiBuilder=new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Firebase Cloud Messaging")
.setContentText(body)
.setAutoCancel(true)
.setSound(notificationsound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0/*Id of notification*/,notifiBuilder.build());
}
}
SecondActivity.java
public class SecondActivity extends AppCompatActivity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv2);
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:id="@+id/imageView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="151dp"
android:src="@drawable/firebase"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Token"
android:id="@+id/buttonshowtoken"
android:layout_below="@+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="49dp" />
</RelativeLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="250dp"
android:textSize="20dp"/>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.abc.firebasenoteg" >
<uses-permission android:name="ANDROID.PERMISSION.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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=".SecondActivity"/>
<service
android:name=".MyFirebaseMessagingService"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service
android:name=".MyFirebaseInstanceIDService"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
</application>
</manifest>
答案 0 :(得分:0)
您需要从服务器请求中单击ClickListener,并在您的活动中的清单intent-action中添加侦听器名称。
"notification" : {
"body" : "Body",
"title" : "Titme",
"icon" : "YourIcon"
"click_action" : "YourActivity"
}
<activity
android:name=".SecondActivity">
<intent-filter>
<action android:name="YourActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
希望这有用,谢谢